Are you supposed to use self
when referencing a member function in Python (within the same module)?
More generally, I was wondering when it is required to use self
, not just for methods but for variables as well.
Adding an answer because Oskarbi's isn't explicit.
You use self
when:
- Defining an instance method. It is passed automatically as the first parameter when you call a method on an instance, and it is the instance on which the method was called.
- Referencing a class or instance attribute from inside an instance method. Use it when you want to call a method or access a name (variable) on the instance the method was called on, from inside that method.
You don't use self
when
- You call an instance method normally. Using Oskarbi's example, if you do
instance = MyClass()
, you callMyClass.my_method
asinstance.my_method(some_var)
not asinstance.my_method(self, some_var)
. - You reference a class attribute from outside an instance method but inside the class definition.
- You're inside a staticmethod.
These don'ts are just examples of when not to use self. The dos are when you should use it.
Use self
to refer to instance variables and methods from other instance methods. Also put self
as the first parameter in the definition of instance methods.
An example:
class MyClass(object):
my_var = None
def my_method(self, my_var):
self.my_var = my_var
self.my_other_method()
def my_other_method(self):
# do something...
There is nothing 'special' about the name
self
. It is the name preferred by convention by Pythonistas, to indicate what that parameter is expected to contain.The Python runtime will pass a 'self' value when you call an instance method on an instance, whether you deliberately provide for it or not. This will usually result in an easily diagnosed/understood error (since the function will get called with the wrong number of parameters), but the use of
*args
can lead to rather more strange type errors.The parameter is passed implicitly when you call an instance method on an instance. It contains the instance upon which you call the method. So you don't mention
self
in the function call because (a) as noted above, that wouldn't make any sense (there isn't aself
in scope, in general, andself
is not a keyword or special name or anything); (b) you've already indicated the instance to use (by writingmy_instance.
).You can, of course, explicitly call an instance method by accessing it from the class. In this case, you'll need to pass the instance explicitly as the first parameter. You generally speaking don't want to do this. And you especially don't want to write code that considers the possibility that the first parameter is something else that's been explicitly passed in this way. This is akin to checking
if (this == null)
in C++: you don't do it, because if it could possibly mean anything, then the calling code is wrong, morally if not legally. (At least in Python you won't have problems with undefined behaviour, but it's still morally wrong.)Within the instance method, since
self
is a parameter which has been assigned the instance as a value, you can writeself.whatever
to access attributes of the instance. Unlike in some other 'implicitthis
' style languages, the attribute names are not implicitly "in scope".There are no other use cases for
self
, since again it's not a special name, and that is the one specific purpose that the naming convention addresses. If you needed to access a 'variable' (really an attribute) from another module, you would use the module name. If you wanted to access one from the current module, no prefix is needed, or really possible for that matter. (Well, you could explicitly look it up in thedict
returned byglobals()
, but please don't do that.)
For instance variable and for methods it is mandatory to use self anytime.
精彩评论