What is method?
function is a piece of code that can be called by name, and we can pass parameters in to get the return value. All parameters are explicitly passed in the past.
method is the combination of function and object. When we call a method, some parameters are implicitly passed in the past. This is detailed below.
Instancemethod
In [5]: Class Human (object): ...: def __init__ (self, weight): ...: self.weight = Weight ... : def get_weight (self): ...: return self.weight ...: in [6]: Human.get_weightout[6]:
This tells us that Get_weight is a method that is not bound, what is called unbound? Keep looking.
In [7]: Human.get_weight ()---------------------------------------------------------------------------TypeError Traceback (most recent)/home/yao/learn/insight_python/
In
()----> 1 human.get_weight () Typeerror:unbound method Get_weight () must is called with Human Instance as first argument (got nothing instead)
An unbound method must be called with a human instance as the first argument. Let's try it.
In [ten]: Human.get_weight (Human) out[10]: 45
Sure enough, but in general we are used to it.
In [all]: Person = Human (a) in []: Person.get_weight () out[12]: 45
The results of these two methods are identical. Let's look at the official documentation to explain the phenomenon.
When an instance attribute was referenced that isn ' t a data attribute, it class is searched.
If the name denotes a valid class attribute is a function object and a method object is
Created by packing (pointers to) The instance object and the function object just found together
In a abstract Object:this is the method object. When the method object was called with an
Argument list, a new argument list is constructed from the instance object and the argument list,
And the function object is a called with this new argument list.
The usual invocation method (Person.get_weight ()) is to pass the instance of the call hidden as a parameter self passed, and self is just an ordinary parameter name, not a keyword.
in [+]: person.get_weightout[13]:
<__main__.human object="" at="" 0x8e13bec="">
> in []: personout[14]: <__main__. Human at 0x8e13bec>
We see that get_weight is bound to the instance object of person.
Summarized under
- Instance method is the combination of an instance object and a function.
- Using the class invocation, the first parameter explicitly passes past an instance.
- With an instance invocation, the instance of the invocation is passed implicitly as the first parameter.
Classmethod
In [1]: Class Human (object): ...: weight = ...: @classmethod .... def get_weight (CLS): ...: return cls.weight in [2]: human.get_weightout[2]:
>
We see that Get_weight is a method that binds to the Human class. Call to see below
In [3]: Human.get_weight () out[3]: 12In [4]: Human (). Get_weight () out[4]: 12
Both classes and instances of the class can call Get_weight and call the results exactly as they are.
We see that weight is a property of the Human class and, of course, an instance of Human. Is the parameter that passes past the CLS a class or an instance?
In [1]: Class Human (object): ...: weight = ...: @classmethod .... def get_weight (CLS): ...: print CLS in [2]: Human.get_weight () in
[3]: Human (). Get_weight ()
We see that passing past is the Human class, not an instance of Human, and the result of the two-way invocation is no different. The CLS is just a normal function parameter that is implicitly passed through when invoked.
Sum up
- Classmethod is a combination of class objects and functions.
- You can invoke an instance of a class and a class, but you pass the class as an implied parameter past.
- Using a class to invoke Classmethod avoids the overhead of instantiating a class.
Staticmethod
In [1]: Class Human (object): ...: @staticmethod ...: def add (A, B): ...: return a + b ...: def get_weight (self): ...: return Self.add (1, 2) in [2]: human.addout[2]: in
[3]: Human () . addout[3]: in
[4]: Human.add (1, 2) out[4]: 3 in [5]: Human (). Add (1, 2) out[5]: 3
We see that add is just a normal function on both a class and an instance and is not bound to any particular class or instance. It can be called with an instance of a class or class, and there are no implicit arguments to pass in.
In [6]: Human (). Add was Human (). Addout[6]: True in [7]: Human (). Get_weight is Human (). Get_weightout[7]: False
Add is also the same object on two instances. Instancemethod is different, and each time a new Get_weight object is created.
Summarized under
- You can use Staticmethod when a function logically belongs to a class and does not depend on the properties of the class.
- You can use Staticmethod to avoid the overhead of creating an object each time you use it.
- Staticmethod can be called using instances of classes and classes. But not dependent on the state of the class and the instance of the class.