A detailed explanation of the parameter passing in Python method

Source: Internet
Author: User

A detailed explanation of the parameter passing in Python 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_we Ight (self): ...: Return self.weight ...: in [6]: Human.get_weight out[6]: <unbound method Human.get_weight >
this tells us that Get_weight is a method that has not been bound
In [7]: Human.get_weight ()--------------------------------------------------------------------------- TypeError Traceback (most recent)/home/yao/learn/insight_python/<ipython-input-7-a2b2c5cd2f8 D> in <module> ()----> 1 human.get_weight () Typeerror:unbound method Get_weight () must BES called with Huma n Instance as first argument (got nothing instead)
An unbound method must use a human instance as the first parameter to invoke a
In [ten]: Human.get_weight (Human) out[10]:
In [all]: Person = Human (a) in []: Person.get_weight () out[12]:
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_weight out[13]: <bound method Human.get_weight of <__main__. Human object at 0x8e13bec>> in [+]: Person out[14]: <__main__. Human at 0x8e13bec>


we see that get_weight is bound to the instance object of person.
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_weight out[2]: <bound method Type.get_weight of <class ' __main__. Human ' >>
we see that Get_weight is a method that binds to the Human class.
In [3]: Human.get_weight () out[3]: 12In [4]: Human (). Get_weight () out[4]:
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 () <class ' __main__. Human ' > in [3]: Human (). Get_weight () <class ' __main__. Human ' >
passing past is a 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.
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_w Eight (self): ...: Return Self.add (1, 2) in [2]: Human.add out[2]: <function __main__.add> in [3]: Human ( ). Add out[3]: <function __main__.add> in [4]: Human.add (1, 2) out[4]: 3 in [5]: Human (). Add (1, 2) out[5]: 3
add is just a normal function on either a class or 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 (). Add out[6]: True in [7]: Human (). Get_weight is Human (). Get_weight out[7]: False

Add is also the same object on two instances. Instancemethod is different, and each time a new Get_weight object is created.

This article is from the "madefake145" blog, make sure to keep this source http://10078785.blog.51cto.com/10068785/1627896

A detailed explanation of the parameter passing in Python method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.