whats self in python methods and classes ?

Home Forums Python Programming whats self in python methods and classes ?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1931 Reply

    I am always confused with ‘self’ keyword which I see in python code. Can you please help me to understand it ?

    #1963 Reply
    Humble
    Keymaster

    Sorry for being late here 🙂 ..

    Please remember : ‘self’ have to be defined ‘explicitly’ in every method of a class.

    The first argument of every class method, including __init__, is always a reference to the current instance of the class. By convention, this argument is always named self.
    ‘self’ is basically a temporary place holder for the object.

    As you know, the class can have __init__ method to start with. In the __init__ method, ‘self’ refers to the newly created object; in other class methods, it refers to the instance whose method was called. Because python works on ‘scopes’ and its bound to ‘objects’ or ‘instances’.

    Although you need to specify ‘self’ explicitly when defining the method, you do not specify it when calling the method; Python will add it for you automatically.

    Because of ‘self’ you have the capability of creating multiple objects from the same class.

    I hope it helps .. Please let me know if you have any doubts.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: whats self in python methods and classes ?
Your information: