Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can python have class or instance methods that do not have "self" as the first argument? [duplicate]

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.

like image 746
bob.sacamento Avatar asked Jan 14 '13 22:01

bob.sacamento


People also ask

Do all Python class methods need self?

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.

Can we create class without self in Python?

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!

Why is self The first argument in Python?

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.

Can class method access self in Python?

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 .

What is the difference between self and instance in Python?

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?

What is self argument in Python?

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.

What is the use of class self in Python?

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.

Is it possible to have self as the first parameter in Python?

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.


2 Answers

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)    
like image 170
abarnert Avatar answered Oct 14 '22 11:10

abarnert


static methods don't need self, they operate on the class

see a good explanation of static here: Static class variables in Python

like image 22
neu-rah Avatar answered Oct 14 '22 11:10

neu-rah