Python and the use of self in methods

From what I read / understand, the parameter "I" is similar to 'this'.

It's true?

If this is not necessary, what would you do if I were not passed to the method?

+6
python self
source share
6 answers

Yes, it was used in a similar way. Note that this is a positional parameter, and you can call it what you want; however, there is a strong agreement to call it self (not this or something else). Some positional parameter must be there for the useful instance method; It is optional.

+5
source share

Joy of Python

This is true to some extent. Methods are associated with the instance of the object of which they are a part. When you see

def some_func(self, foo, bar)

Passing self is sometimes implicit when you call, for example:

obj.some_func(foo_val, bar_val)

Which is equal (it is assumed that obj has a class MyClass ) to

MyClass.some_func(obj, foo_val, bar_val)

Since the method is bound to obj , the argument self populated. This is part of Python, explicit with what it means. In other languages, this just appears in scope, and Python discovers how this happens.

You can also pass methods and manually pass them self when not called from the associated context.

Python docs do a good job :

 xf = xf 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 xf () was called> without the argument above, although the function definition for f () specified the argument>. What happened to the argument? Of course, Python throws an exception when the> function requiring an argument is called without any - even if the argument is not actually used ...

Actually, you may have guessed the answer: a special thing about methods is that the> object is passed as the first argument to the function. In our example, calling xf () is exactly equivalent to MyClass.f (x). In the general case, calling a method with a list of n arguments> is equivalent to calling a corresponding function with a created list of arguments> by inserting a method object before the first argument.

+3
source share

self is this , you just have to explicitly pass it and use it explicitly to refer to the methods / properties of the class.

It is not optional in class methods. You will get a TypeError if you try to define a class method 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.

+2
source share

In classes, the variable a self (or cls for classmethods) is required. However, you want to call it your decision. If you prefer, you could call it this .

A classmethod is a method that receives class as the first argument instead of an instance. It can be called without passing an instance. those. using classmethod you can:

 SomeObject.some_class_method() 

while the usual method will require you to execute

 SomeObject().some_normal_method() or SomeObject.some_normal_method(instance) 
0
source share

self refers to the object on which the method was called, like this in C ++. But it is important that self be just a convention, you can name it whatever you want and pass instances of subclasses.

0
source share

self definitely similar to this , but in Python, the name self is just a convention and can be called something else. The variable is named after what you name it in the function prototype ( def function(whatever, params...): .

For example, self methods are really necessary. For a class or static methods, you need to specify that they should be treated as such, and then self 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 can never use the instance method in such a way that self is not passed. For example, if I have an instance of the my_car class of the Car class, and I use the Car drive class, the instance of my_car will be implicitly passed to 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) 
0
source share

All Articles