Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - self, no self and cls

Yet another question on what the 'self' is for, what happens if you don't use 'self' and what's 'cls' for. I "have done my homework", I just want to make sure I got it all.

self - To access an attribute of an object, you need to prefix the attribute name with the object name (objname.attributename). The same way self is used to access an attribute inside the object (class) itself. So if you didn't prefix a variable with self in a class method, you wouldn't be able to access that variable in other methods of the class, or outside of the class. So you could omit it if you wanted to make the variable local to that method only. The same way if you had a method and you didn't have any variable you wanted to share with other methods, you could omit the self from the method arguments.

cls - Each instance creates it's own "copy" of the attributes, so if you wanted all the instances of a class to share the same variable, you would prefix that variable name with 'cls' in the class declaration.

Is this all right? Thanks.

like image 919
Joelmc Avatar asked Sep 26 '11 11:09

Joelmc


People also ask

What is difference between CLS and self in Python?

cls refers to the class, whereas self refers to the instance. Using the cls keyword, we can only access the members of the class, whereas using the self keyword, we can access both the instance variables and the class attributes. With cls, we cannot access the instance variables in a class.

What does CLS () do in Python?

cls accepts the class Person as a parameter rather than Person's object/instance. Now, we pass the method Person. printAge as an argument to the function classmethod . This converts the method to a class method so that it accepts the first parameter as a class (i.e. Person).

What are the 3 types of methods in Python?

There are basically three types of methods in Python: Instance Method. Class Method. Static Method.

Can I use self in Classmethod Python?

Self is a convention and not a Python keyword . self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.


2 Answers

The same way self is used to access an attribute inside the object (class) itself.

Not inside the object / class, just inside the class' instance methods. self is just a convention, you could call it whatever you wanted, even something different in each method.

So if you didn't prefix a variable with self in a class method, you wouldn't be able to access that variable in other methods of the class, or outside of the class.

self is used in instance methods, cls is often used in class methods. Otherwise, correct.

So you could omit it if you wanted to make the variable local to that method only.

Yes, inside a method a variable name is like inside any other function -- the interpreter looks for the name locally, then in closures, then in the globals / module level, then in the Python built-ins.

The same way if you had a method and you didn't have any variable you wanted to share with other methods, you could omit the self from the method arguments.

No, you can't just omit "self" from the method arguments. You have to tell Python you want a staticmethod, which won't automatically get passed the instance of the class, ether by doing @staticmethod above the def line, or mymethod = staticmethod(mymethod) below the method body.

Each instance creates it's own "copy" of the attributes, so if you wanted all the instances of a class to share the same variable, you would prefix that variable name with 'cls' in the class declaration.

Inside the class definition, but outside any methods, names are bound to the class -- that's how you define methods etc. You don't prefix them with cls or anything else.

cls is generally used in the __new__ special staticmethod, or in classmethods, which you make similarly to staticmethods. These are methods that only need access to the class, but not to things specific to each instance of the class.

Inside a classmethod, yes, you'd use this to refer to attributes you wanted all instances of the class, and the class itself, to share.

Like self, cls is just a convention, and you could call it whatever you wanted.

A brief example:

class Foo(object):      # you couldn't use self. or cls. out here, they wouldn't mean anything      # this is a class attribute     thing = 'athing'      def __init__(self, bar):         # I want other methods called on this instance of Foo         # to have access to bar, so I create an attribute of self         # pointing to it         self.bar = bar      @staticmethod     def default_foo():         # static methods are often used as alternate constructors,         # since they don't need access to any part of the class         # if the method doesn't have anything at all to do with the class         # just use a module level function         return Foo('baz')      @classmethod     def two_things(cls):         # can access class attributes, like thing         # but not instance attributes, like bar         print cls.thing, cls.thing 
like image 173
agf Avatar answered Sep 29 '22 08:09

agf


You use self as the first argument in regular methods where the instance is passed automatically through this argument. So whatever the first argument is in a method - it points to the current instance

When a method is decorated with @classmethod it gets the class passed as its first argument so the most common name for it is cls as it points to the class.

You usually do not prefix any variables (hungarian notation is bad).


Here's an example:

class Test(object):     def hello(self):         print 'instance %r says hello' % self     @classmethod     def greet(cls):         print 'class %r greet you' % cls 

Output:

>>> Test().hello() instance <__main__.Test object at 0x1f19650> says hello  >>> Test.greet() class <class '__main__.Test'> greet you 
like image 28
ThiefMaster Avatar answered Sep 29 '22 07:09

ThiefMaster