Python - self, no self and cls Python - self, no self and cls python python

Python - self, no self and cls


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


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