|
- What is the purpose of the `self` parameter? Why is it needed?
For a language-agnostic consideration of the design decision, see What is the advantage of having this self pointer mandatory explicit? To close debugging questions where OP omitted a self parameter for a method and got a TypeError, use TypeError: method () takes 1 positional argument but 2 were given instead If OP omitted self in the body of the method and got a NameError, consider How can
- oop - Why do you need explicitly have the self argument in a Python . . .
By making the self reference explicit, you're free to refer to any object by that self reference Also, such a way of playing with classes at runtime is harder to do in the more static languages - not that's it's necessarily good or bad It's just that the explicit self allows all this craziness to exist
- What difference does it make to use self to define a member in a . . .
A x is a class variable B 's self x is an instance variable i e A 's x is shared between instances It would be easier to demonstrate the difference with something that can be modified like a list:
- Python self keyword - Stack Overflow
9 First, Python's self is not a keyword, it's a coding convention, the same as Python's cls Guido has written a really detailed and valuable article about the origin of Python's support for class, and in that article, Guido explains why use self and cls, and why they are necessary
- Why do I get TypeError: Missing 1 required positional argument: self?
See Why do I get 'takes exactly 1 argument (2 given)' when trying to call a method? for the opposite problem
- Difference between this and self in JavaScript - Stack Overflow
Everyone is aware of this in javascript, but there are also instances of self encountered in the wild, such as here So, what is the difference between this and self in JavaScript?
- Purpose of return self python - Stack Overflow
Returning self from a method simply means that your method returns a reference to the instance object on which it was called This can sometimes be seen in use with object oriented APIs that are designed as a fluent interface that encourages method cascading
- Python class methods: when is self not needed - Stack Overflow
17 What is self? In Python, every normal method is forced to accept a parameter commonly named self This is an instance of class - an object This is how Python methods interact with a class's state You are allowed to rename this parameter whatever you please but it will always have the same value:
|
|
|