python tutorial - Python - class method vs static method - learn python - python programming
Methods
- Method is just a function object created by def statement.
def outside_foo():
click below button to copy the code. By Python tutorial team
class method
- If we need to use class attributes
@classmethod
def cfoo(cls,)
click below button to copy the code. By Python tutorial team
static method
- We do not have any info about the class
@staticmethod
def sfoo()
click below button to copy the code. By Python tutorial team
Instance method
- Instance method but inner working of instance method is the same as the class method. Actually, Python automatically maps an instance method call to a class method.
instance.method(args...)
click below button to copy the code. By Python tutorial team
Class Method and Instance Method
- The method foo() is designed to process instances, we normally call the method through instance:
>>> from a import A
>>> a = A()
>>> a.foo('instance call')
instance call
click below button to copy the code. By Python tutorial team
Note: that we pass the instance a as an argument of the foo() method.
1:Bound method (instance call):
- To call the method we must provide an instance object explicitly as the first argument. In other words, a bound method object is a kind of object that remembers the self instance and the referenced function.
- Python automatically packages the instance with the function in the bound method object, so we don't need to pass an instance to cal the method.
>>> class Callback:
... def __init__(self, color):
... self.color = color
... def changeColor(self):
... print(self.color)
...
>>> obj = Callback('red')
>>> cb = obj.changeColor
>>> cb()
red
click below button to copy the code. By Python tutorial team
2:Unbound method (class call):
- Accessing a function attribute of a class by qualifying the class returns an unbound method object:
>>> Callback.changeColor
<unbound method Callback.changeColor>
click below button to copy the code. By Python tutorial team
Note: In Puython 3.0, the notion of unbound method has been dropped, and what we describe as an unbound method here is treated as a simple function in 3.0.
Static Method
- Static methods are used when we need to process data associated with classes instead of instances. A static method has no self argument and it is nested in a class and is designed to work on class attributes instead of instance attributes.
- I'll use function decorator(@) for static method, and the syntax looks like this:
class S:
@staticmethod
def foo():
...
click below button to copy the code. By Python tutorial team
- Internally, the definition has the same effect as the name rebinding:
class S:
def foo():
...
foo = staticmethod(foo)
click below button to copy the code. By Python tutorial team
- Suppose we have a typical instance counting code like this:
# s.py
class S:
nInstances = 0
def __init__(self):
S.nInstances = S.nInstances + 1
@staticmethod
def howManyInstances():
print('Number of instances created: ', S.nInstances)
click below button to copy the code. By Python tutorial team
- Then, we make three instances:
>>> from s import S
>>>
>>> a = S()
>>> b = S()
>>> c = S()
click below button to copy the code. By Python tutorial team
- Now that we have static method, we can call it in two ways:
- call from class
- call from instances
>>> S.howManyInstances()
('Number of instances created: ', 3)
>>> a.howManyInstances()
('Number of instances created: ', 3)
click below button to copy the code. By Python tutorial team
Instance, static, and class methods
- Here is a simple code that has all types of methods:
class Methods:
def i_method(self,x):
print(self,x)
def s_method(x):
print(x)
def c_method(cls,x):
print(cls,x)
s_method = staticmethod(s_method)
c_method = classmethod(c_method)
obj = Methods()
obj.i_method(1)
Methods.i_method(obj, 2)
obj.s_method(3)
Methods.s_method(4)
obj.c_method(5)
Methods.c_method(6)
click below button to copy the code. By Python tutorial team
Output:
(<__main__.Methods instance at 0x7f7052d75950>, 1)
(<__main__.Methods instance at 0x7f7052d75950>, 2)
3
4
(<class __main__.Methods at 0x7f7052d6d598>, 5)
(<class __main__.Methods at 0x7f7052d6d598>, 6)