static, class and instance methods

ast nomail at com.invalid
Thu Oct 6 02:53:53 EDT 2016


Hello,

In a class there are three possible types of methods,
the static methods, the class methods and the
instance methods

* Class methods are decorated, eg:

@classmethod
def func(cls, a, b):
...

I read that the decorator tranforms 'func' as a descriptor,
and when this descriptor is read, it provided a new
function 'func' where the 1st parameter cls is already filled 
with the class. That's ok for me.


* For instance methods, there is no decorator:

def funct2(self, a, b):
...

self is automatically filled with the instance when we call
funct2 from an instance and not filled if funct2 is called from
a class.
But there is no decorator, why ? Is python doing the conversion
of funct2 to a descriptor itself, behind the scene ?


* static methods are decorated too

@staticmethod
def funct3(a, b):
...

The 1st argument is not supposed to be automatically filled
So what the decorator used for ? 
Just to distinguish funct3 from an instance method ?

regards







More information about the Python-list mailing list