+ 1
Code Problem call of a class
I've faired a challenge in that the question of the attended code at the end of myh text came up. It's always so that I can call a class with less parameters in the class call as the constructor of the class (__init__) needs? And if I can do this, than the parameter input of the call contains the last parameters, not the first? I don't have contact with this information during learning python. https://code.sololearn.com/cqHmgzbuUle5/
3 Réponses
+ 2
Not sure I understand your questions. I will try to explain what is happening in your code.
In your code, the __init__ function has three parameters: a,b, and c
While not strictly required, the __init__ constructor should have self
In your code, you have "a" instead of self
When you instantiate this class (i.e. make an instance of the class) with the A(8,3) statement, the first parameter is the self parameter, and the second and third parameters can be passed to the function.
#What's the output of this code?
class A:
def __init__(a,b,c):
print(b)
A(8,3)
Try this alternative to your code:
#What's the output of this code?
class A:
def __init__(self,b,c):
print(b)
print(c)
A(8,3)
The output is
8
3
The first parameter, self, is implicit. In other words, you do not pass a value to it.
For more of an explanation on self, go to
Python Tips on self: https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/
StackOverflow on self: https://stackoverflow.com/questions/6990099/explaining-the-python-self-variable-to-a-beginner
+ 1
You're welcome!
0
Thank you for your nice explanation. This was my problem. I don't have known that if self isn't explicit referenced, that than the first parameter is set as the self parameter!