+ 5

What is happening here?

class A(): def __init__(a, b, c): print(b) A(8,3) #output: 8 Why is the output 8? At first I thought because the argument “a” is a substitute for “self”. Seeing that self is not a keyword, it can be anything. But when I run it like this class A(): def __init__(a, b, c): print(b) A(8,3,4) The debug catches it and says TypeError: Def __init__ Takes 3 positional arguments but 4 were given. I am at a loss.

6th Jul 2019, 5:48 PM
Brave Tea
Brave Tea - avatar
3 Respostas
+ 8
figured it out. a (normally ‘self’) is given implicitely by Python, that is why 4 are given.
6th Jul 2019, 5:51 PM
Brave Tea
Brave Tea - avatar
+ 2
In your first option 'a' is 'self', and you gave values '8' and '3' to arguments 'b' and 'c' respectively. That is why the output 'b' is '8'.
7th Jul 2019, 10:13 AM
Антон Попов
Антон Попов - avatar
0
thanks, yeah that is what I understood about it in my answer :) and self is implicitely given along, that was what confused me.0
7th Jul 2019, 10:34 AM
Brave Tea
Brave Tea - avatar