Possible Duplicate:
Why do you need explicitly have the “self” argument into a Python method?
Python ‘self’ explained
This is just for my own edification. I am learning python and have moved into OOP with python. Every single example of a method in a class that I have seen has "self" as the first argument. Is this true of all methods? If it is true, couldn't python have been written so that this argument was just understood and therefore not needed? Thanks.
self is the first method of every Python class When you define a class in Python, every method that you define, must accept that instance as its first argument (called self by convention). The self variable points to the instance of the class that you're working with.
Class methods don't need self as an argument, but they do need a parameter called cls. This stands for class, and like self, gets automatically passed in by Python. Class methods are created using the @classmethod decorator. print('Hello, World!
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. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self .
With the self keyword, we can access the attributes. In python, every method created inside a class is by default considered as an instance method unless specified explicitly. There is no need for a decorator for this method. With the instance method, we can access the data for each instance of the class. What is self in class?
Self is the first argument to be passed in Constructor and Instance Method. Self must be provided as a First parameter to the Instance method and constructor. If you don’t provide it, it will cause an error. 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.
However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods.
If you have been programming in Python (object-oriented programming) for some time, then you have definitely come across methods that have self as their first parameter. Let us first try to understand what this recurring self parameter is.
If you want a method that doesn't need to access self
, use staticmethod
:
class C(object):
def my_regular_method(self, foo, bar):
pass
@staticmethod
def my_static_method(foo, bar):
pass
c = C()
c.my_regular_method(1, 2)
c.my_static_method(1, 2)
If you want access to the class, but not to the instance, use classmethod
:
class C(object):
@classmethod
def my_class_method(cls, foo, bar):
pass
c.my_class_method(1, 2)
static methods don't need self, they operate on the class
see a good explanation of static here: Static class variables in Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With