From what I read/understand, the 'self' parameter is similiar to 'this'.
Is that true?
If its optional, what would you do if self wasnt' passed into the method?
From what I read/understand, the 'self' parameter is similiar to 'this'.
Is that true?
If its optional, what would you do if self wasnt' passed into the method?
In classes a self
variable (or cls
for classmethods) is required. What you want to call it is your decision though. If you prefer you could call it this
instead.
A classmethod
is a method that gets the class
as a first argument instead of a instance. It can be called without passing an instance.
i.e. with a classmethod
you can do:
SomeObject.some_class_method()
while a normal method would require you to do
SomeObject().some_normal_method()
or
SomeObject.some_normal_method(instance)
Yes, it's used in similar ways. Note that it's a positional parameter and you can call it what you want; however there is a strong convention to call it self
(not this
or anything else). Some positional parameter must be there for a usable instance method; it is not optional.
self
is this
, just you have to explicitly pass it and explicitly use it to refer to class methods/properties.
It isn't optional in class methods. You will get a TypeError
if you try to define a classmethod without at least one argument (i.e., the self
parameter).
However, you can call it something other than self
, but I have never seen otherwise.
self
refers to the object on which the method was called, much like this
in C++. But it is important that self
is merely a convention, you can name it as you like and pass instances of subclasses.
The joy of Python
That is true to some extend. Methods are bound to the object instance they are a part of. When you see
def some_func(self, foo, bar)
The passing of self is sometimes implicit when you call, for example:
obj.some_func(foo_val, bar_val)
Which is equal (presuming obj
is of class MyClass
) to
MyClass.some_func(obj, foo_val, bar_val)
Because the method is bound to obj
, the self
argument gets populated. This is part of Python being explicit with what it means. In other languages, this
just pops into scope, with Python there is some exposure of how this happens.
You can also pass methods around, and manually pass them self
when not calling from a bound context.
The Python docs do a good Job:
xf = x.f
while True:
print xf()
will continue to print hello world until the end of time.
What exactly happens when a method is called? You may have noticed that x.f() was called >without an argument above, even though the function definition for f() specified an >argument. What happened to the argument? Surely Python raises an exception when a function >that requires an argument is called without any — even if the argument isn’t actually >used...
Actually, you may have guessed the answer: the special thing about methods is that the >object is passed as the first argument of the function. In our example, the call x.f() is >exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments >is equivalent to calling the corresponding function with an argument list that is created >by inserting the method’s object before the first argument.
self
is definitely similar to this
, however, in Python, the name self
is just a convention, and could be named anything else. The variable is named after whatever you call it in the function's prototype (def function(whatever, params...):
).
For instance methods, self
IS actually required. For class or static methods, you need to specify that they should be treated as such, and then self
is not required. For example:
def type_to_display(type):
"""Converts a pass type to the full written pass type."""
return list((pair[1] for pair in Pass.TYPE_CHOICES if pair[0] ==
type[0:1].upper()))[0]
type_to_display = staticmethod(type_to_display)
You will never be able to use an instance method in such a way that self is not passed in. For example, if I have an instance my_car
of a Car
class, and I use the Car
class's drive
instance method, the my_car
instance will be implicitly passed into the drive
method as the first parameter (self
).
class Car:
def drive(self):
self.do_some_stuff()
my_car = Car()
my_car.drive() # actually calls Car.drive(my_car)