254

What is the difference between the following class methods?

Is it that one is static and the other is not?

class Test(object):
  def method_one(self):
    print "Called method_one"

  def method_two():
    print "Called method_two"

a_test = Test()
a_test.method_one()
a_test.method_two()
Abdulaziz
  • 13,694
  • 3
  • 13
  • 12
Franck Mesirard
  • 3,169
  • 3
  • 20
  • 17
  • 19
    No difference other than method_two() definition is invalid and its call fails. – anatoly techtonik Jul 22 '12 at 13:00
  • 18
    @techtonik: Nothing is wrong with the definition of the method_two! It's being called in an incorrect/invalid spec, i.e. with an extra argument. – 0xc0de Sep 10 '13 at 08:36
  • 2
    Yours are both **instance methods**, not class methods. You create a [class method](https://docs.python.org/3/library/functions.html?highlight=classmethod#classmethod) by applying `@classmethod` to the definition. The first parameter should be called `cls` instead of `self` and will receive the class object rather than an instance of your class: `Test.method_three()` and `a_test.method_three()` are equivalent. – Lutz Prechelt Aug 04 '16 at 14:50
  • Why would you want to create a function definition without the `self` argument? Is there a strong use case for this? – alpha_989 Jan 11 '18 at 14:49

13 Answers13

437

In Python, there is a distinction between bound and unbound methods.

Basically, a call to a member function (like method_one), a bound function

a_test.method_one()

is translated to

Test.method_one(a_test)

i.e. a call to an unbound method. Because of that, a call to your version of method_two will fail with a TypeError

>>> a_test = Test() 
>>> a_test.method_two()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: method_two() takes no arguments (1 given) 

You can change the behavior of a method using a decorator

class Test(object):
    def method_one(self):
        print "Called method_one"

    @staticmethod
    def method_two():
        print "Called method two"

The decorator tells the built-in default metaclass type (the class of a class, cf. this question) to not create bound methods for method_two.

Now, you can invoke static method both on an instance or on the class directly:

>>> a_test = Test()
>>> a_test.method_one()
Called method_one
>>> a_test.method_two()
Called method_two
>>> Test.method_two()
Called method_two
Community
  • 1
  • 1
Torsten Marek
  • 83,780
  • 21
  • 91
  • 98
  • 21
    I support this answer, it is superior to mine. Well done Torsten :) – freespace Sep 22 '08 at 11:09
  • 27
    in python 3 unbound methods are deprecated. instead there is just a function. – boldnik May 30 '13 at 20:39
  • 1
    @boldnik, why do you say unbound methods are deprecated? static methods are still present in the documentation: https://docs.python.org/3/library/functions.html#staticmethod – alpha_989 Jan 11 '18 at 15:07
  • @alpha_989: (old comment, but anyway) see https://stackoverflow.com/a/11950080 for details. – djvg Apr 21 '21 at 12:29
  • and [Python 3.0 release notes](https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods) – djvg Apr 21 '21 at 12:40
206

Methods in Python are a very, very simple thing once you understood the basics of the descriptor system. Imagine the following class:

class C(object):
    def foo(self):
        pass

Now let's have a look at that class in the shell:

>>> C.foo
<unbound method C.foo>
>>> C.__dict__['foo']
<function foo at 0x17d05b0>

As you can see if you access the foo attribute on the class you get back an unbound method, however inside the class storage (the dict) there is a function. Why's that? The reason for this is that the class of your class implements a __getattribute__ that resolves descriptors. Sounds complex, but is not. C.foo is roughly equivalent to this code in that special case:

>>> C.__dict__['foo'].__get__(None, C)
<unbound method C.foo>

That's because functions have a __get__ method which makes them descriptors. If you have an instance of a class it's nearly the same, just that None is the class instance:

>>> c = C()
>>> C.__dict__['foo'].__get__(c, C)
<bound method C.foo of <__main__.C object at 0x17bd4d0>>

Now why does Python do that? Because the method object binds the first parameter of a function to the instance of the class. That's where self comes from. Now sometimes you don't want your class to make a function a method, that's where staticmethod comes into play:

 class C(object):
  @staticmethod
  def foo():
   pass

The staticmethod decorator wraps your class and implements a dummy __get__ that returns the wrapped function as function and not as a method:

>>> C.__dict__['foo'].__get__(None, C)
<function foo at 0x17d0c30>

Hope that explains it.

Armin Ronacher
  • 31,998
  • 13
  • 65
  • 69
  • 14
    *The `staticmethod` decorator wraps your class (...)* This phrase is a bit misleading as the class which is being wrapped is the class **of** the method `foo` and not the class in which `foo` is defined. – Piotr Dobrogost Jun 25 '13 at 09:42
  • 1
    In Python 3, `C.foo is C.__dict__['foo']`. From the [Python 3.0 release notes](https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods): "The concept of 'unbound methods' has been removed from the language. When referencing a method as a class attribute, you now get a plain function object." – djvg Apr 21 '21 at 12:41
13

When you call a class member, Python automatically uses a reference to the object as the first parameter. The variable self actually means nothing, it's just a coding convention. You could call it gargaloo if you wanted. That said, the call to method_two would raise a TypeError, because Python is automatically trying to pass a parameter (the reference to its parent object) to a method that was defined as having no parameters.

To actually make it work, you could append this to your class definition:

method_two = staticmethod(method_two)

or you could use the @staticmethod function decorator.

Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
Justin Poliey
  • 16,289
  • 7
  • 37
  • 48
11
>>> class Class(object):
...     def __init__(self):
...         self.i = 0
...     def instance_method(self):
...         self.i += 1
...         print self.i
...     c = 0
...     @classmethod
...     def class_method(cls):
...         cls.c += 1
...         print cls.c
...     @staticmethod
...     def static_method(s):
...         s += 1
...         print s
... 
>>> a = Class()
>>> a.class_method()
1
>>> Class.class_method()    # The class shares this value across instances
2
>>> a.instance_method()
1
>>> Class.instance_method() # The class cannot use an instance method
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method instance_method() must be called with Class instance as first argument (got nothing instead)
>>> Class.instance_method(a)
2
>>> b = 0
>>> a.static_method(b)
1
>>> a.static_method(a.c) # Static method does not have direct access to 
>>>                      # class or instance properties.
3
>>> Class.c        # a.c above was passed by value and not by reference.
2
>>> a.c
2
>>> a.c = 5        # The connection between the instance
>>> Class.c        # and its class is weak as seen here.
2
>>> Class.class_method()
3
>>> a.c
5
kzh
  • 19,810
  • 13
  • 73
  • 97
  • 2
    `Class.instance_method() # The class cannot use an instance method` it can use. Just pass the instance manually: `Class.instance_method(a)` – warvariuc Jan 19 '12 at 11:35
  • @warwaruk It's there, look at the line below the `TyeError` line. – kzh Jan 20 '12 at 14:06
  • yes i saw it later. still, imo, it's not correct to say 'The class cannot use an instance method', because you just did it one line below. – warvariuc Jan 20 '12 at 16:55
  • @kzh, Thanks for your explanation. When you called `a.class_method()`, it seems `a.c` got updated to `1`, so the call the `Class.class_method()`, updated the `Class.c` variable to `2`. However, when you assigned `a.c=5`, why did `Class.c` not get updated to `5`? – alpha_989 Jan 11 '18 at 15:14
  • @alpha_989 python first looks for an attribute directly on an ovjects own instance first and if it is not there, it looks for it on its class by default. If you have any other questions about this, please feel free to open a question and link it here and I would be happy to help further. – kzh Jan 11 '18 at 15:44
4

method_two won't work because you're defining a member function but not telling it what the function is a member of. If you execute the last line you'll get:

>>> a_test.method_two()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: method_two() takes no arguments (1 given)

If you're defining member functions for a class the first argument must always be 'self'.

Jon Cage
  • 36,366
  • 38
  • 137
  • 215
4

Accurate explanation from Armin Ronacher above, expanding on his answers so that beginners like me understand it well:

Difference in the methods defined in a class, whether static or instance method(there is yet another type - class method - not discussed here so skipping it), lay in the fact whether they are somehow bound to the class instance or not. For example, say whether the method receives a reference to the class instance during runtime

class C:
    a = [] 
    def foo(self):
        pass

C # this is the class object
C.a # is a list object (class property object)
C.foo # is a function object (class property object)
c = C() 
c # this is the class instance

The __dict__ dictionary property of the class object holds the reference to all the properties and methods of a class object and thus

>>> C.__dict__['foo']
<function foo at 0x17d05b0>

the method foo is accessible as above. An important point to note here is that everything in python is an object and so references in the dictionary above are themselves pointing to other objects. Let me call them Class Property Objects - or as CPO within the scope of my answer for brevity.

If a CPO is a descriptor, then python interpretor calls the __get__() method of the CPO to access the value it contains.

In order to determine if a CPO is a descriptor, python interpretor checks if it implements the descriptor protocol. To implement descriptor protocol is to implement 3 methods

def __get__(self, instance, owner)
def __set__(self, instance, value)
def __delete__(self, instance)

for e.g.

>>> C.__dict__['foo'].__get__(c, C)

where

  • self is the CPO (it could be an instance of list, str, function etc) and is supplied by the runtime
  • instance is the instance of the class where this CPO is defined (the object 'c' above) and needs to be explicity supplied by us
  • owner is the class where this CPO is defined(the class object 'C' above) and needs to be supplied by us. However this is because we are calling it on the CPO. when we call it on the instance, we dont need to supply this since the runtime can supply the instance or its class(polymorphism)
  • value is the intended value for the CPO and needs to be supplied by us

Not all CPO are descriptors. For example

>>> C.__dict__['foo'].__get__(None, C)
<function C.foo at 0x10a72f510> 
>>> C.__dict__['a'].__get__(None, C)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__get__'

This is because the list class doesnt implement the descriptor protocol.

Thus the argument self in c.foo(self) is required because its method signature is actually this C.__dict__['foo'].__get__(c, C) (as explained above, C is not needed as it can be found out or polymorphed) And this is also why you get a TypeError if you dont pass that required instance argument.

If you notice the method is still referenced via the class Object C and the binding with the class instance is achieved via passing a context in the form of the instance object into this function.

This is pretty awesome since if you chose to keep no context or no binding to the instance, all that was needed was to write a class to wrap the descriptor CPO and override its __get__() method to require no context. This new class is what we call a decorator and is applied via the keyword @staticmethod

class C(object):
  @staticmethod
  def foo():
   pass

The absence of context in the new wrapped CPO foo doesnt throw an error and can be verified as follows:

>>> C.__dict__['foo'].__get__(None, C)
<function foo at 0x17d0c30>

Use case of a static method is more of a namespacing and code maintainability one(taking it out of a class and making it available throughout the module etc).

It maybe better to write static methods rather than instance methods whenever possible, unless ofcourse you need to contexualise the methods(like access instance variables, class variables etc). One reason is to ease garbage collection by not keeping unwanted reference to objects.

supi
  • 2,172
  • 18
  • 15
1

The call to method_two will throw an exception for not accepting the self parameter the Python runtime will automatically pass it.

If you want to create a static method in a Python class, decorate it with the staticmethod decorator.

Class Test(Object):
  @staticmethod
  def method_two():
    print "Called method_two"

Test.method_two()
MvdD
  • 22,082
  • 8
  • 65
  • 93
1

that is an error.

first of all, first line should be like this (be careful of capitals)

class Test(object):

Whenever you call a method of a class, it gets itself as the first argument (hence the name self) and method_two gives this error

>>> a.method_two()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method_two() takes no arguments (1 given)
hayalci
  • 4,089
  • 2
  • 27
  • 30
1

The second one won't work because when you call it like that python internally tries to call it with the a_test instance as the first argument, but your method_two doesn't accept any arguments, so it wont work, you'll get a runtime error. If you want the equivalent of a static method you can use a class method. There's much less need for class methods in Python than static methods in languages like Java or C#. Most often the best solution is to use a method in the module, outside a class definition, those work more efficiently than class methods.

Vasil
  • 36,468
  • 26
  • 90
  • 114
  • If I define a function `Class Test(object): @staticmethod def method_two(): print(“called method_two”)` One use case, I was thinking of is when you want the function to be a part of a class, but don’t want the user to access the function directly. So the `method_two` can be called by other functions inside the `Test` instance, but cant be called using `a_test.method_two()`. If I use `def method_two()`, will this work for this use case? Or is there a better way to modify the function definition, so it works as intended for the above use case? – alpha_989 Jan 11 '18 at 14:47
1

Please read this docs from the Guido First Class everything Clearly explained how Unbound, Bound methods are born.

James Sapam
  • 16,036
  • 12
  • 50
  • 73
1

Bound method = instance method

Unbound method = static method.

Python Newbie
  • 317
  • 2
  • 9
  • 2
    Please add some further explanation to your answer such that others can learn from it. For example, have a look at the other answers to this question – Nico Haase Jun 01 '20 at 17:51
0

The definition of method_two is invalid. When you call method_two, you'll get TypeError: method_two() takes 0 positional arguments but 1 was given from the interpreter.

An instance method is a bounded function when you call it like a_test.method_two(). It automatically accepts self, which points to an instance of Test, as its first parameter. Through the self parameter, an instance method can freely access attributes and modify them on the same object.

Yossarian42
  • 1,950
  • 17
  • 14
0

Unbound Methods

Unbound methods are methods that are not bound to any particular class instance yet.

Bound Methods

Bound methods are the ones which are bound to a specific instance of a class.

As its documented here, self can refer to different things depending on the function is bound, unbound or static.

Take a look at the following example:

class MyClass:    
    def some_method(self):
        return self  # For the sake of the example

>>> MyClass().some_method()
<__main__.MyClass object at 0x10e8e43a0># This can also be written as:>>> obj = MyClass()

>>> obj.some_method()
<__main__.MyClass object at 0x10ea12bb0>

# Bound method call:
>>> obj.some_method(10)
TypeError: some_method() takes 1 positional argument but 2 were given

# WHY IT DIDN'T WORK?
# obj.some_method(10) bound call translated as
# MyClass.some_method(obj, 10) unbound method and it takes 2 
# arguments now instead of 1 

# ----- USING THE UNBOUND METHOD ------
>>> MyClass.some_method(10)
10

Since we did not use the class instance — obj — on the last call, we can kinda say it looks like a static method.

If so, what is the difference between MyClass.some_method(10) call and a call to a static function decorated with a @staticmethod decorator?

By using the decorator, we explicitly make it clear that the method will be used without creating an instance for it first. Normally one would not expect the class member methods to be used without the instance and accesing them can cause possible errors depending on the structure of the method.

Also, by adding the @staticmethod decorator, we are making it possible to be reached through an object as well.

class MyClass:    
    def some_method(self):
        return self    

    @staticmethod
    def some_static_method(number):
        return number

>>> MyClass.some_static_method(10)   # without an instance
10
>>> MyClass().some_static_method(10)   # Calling through an instance
10

You can’t do the above example with the instance methods. You may survive the first one (as we did before) but the second one will be translated into an unbound call MyClass.some_method(obj, 10) which will raise a TypeError since the instance method takes one argument and you unintentionally tried to pass two.

Then, you might say, “if I can call static methods through both an instance and a class, MyClass.some_static_method and MyClass().some_static_method should be the same methods.” Yes!

Öykü
  • 252
  • 1
  • 3
  • 13