Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
524 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...