Python中method的参数传递详解

Python中method的参数传递是怎么样的呢?今天来研究一下.
function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值。所有的参数都是明确的传递过去的。
method是function与对象的结合。我们调用一个方法的时候,有些参数是隐含的传递过去的。下文会详细介绍。
instancemethod
  In [5]: class Human(object):   ...:   def __init__(self, weight):   ...:     self.weight = weight   ...:   def get_weight(self):   ...:     return self.weight   ...:      In [6]: Human.get_weight Out[6]:  
这告诉我们get_weight是一个没有被绑定方法
 In [7]: Human.get_weight() ---------------------------------------------------------------------------TypeError                 Traceback (most recent call last) /home/yao/learn/insight_python/ in () ----&gt 1 Human.get_weight()    TypeError: unbound method get_weight() must be called with Human instance as first argument (got nothing instead) 
未绑定的方法必须使用一个Human实例作为第一个参数来调用
 In [10]: Human.get_weight(Human(45)) Out[10]: 45 
 In [11]: person = Human(45)    In [12]: person.get_weight() Out[12]: 45 
When an instance attribute is referenced that isn't a data attribute, its class is searched.
If the name denotes a valid class attribute that is a function object, a method object is
created by packing (pointers to) the instance object and the function object just found together
in an abstract object: this is the method object. When the method object is called with an
argument list, a new argument list is constructed from the instance object and the argument list,
and the function object is called with this new argument list.

原来我们常用的调用方法(person.get_weight())是把调用的实例隐藏的作为一个参数self传递过去了, self 只是一个普通的参数名称,不是关键字。

 In [13]: person.get_weight Out[13]: >    In [14]: person Out[14]: <__main__.human at> 


我们看到get_weight被绑定在了 person 这个实例对象上。
    instance method 就是实例对象与函数的结合。 
    使用类调用,第一个参数明确的传递过去一个实例。 
    使用实例调用,调用的实例被作为第一个参数被隐含的传递过去。 
classmethod
  In [1]: class Human(object):   ...:   weight = 12  ...:   @classmethod  ...:   def get_weight(cls):   ...:     return cls.weight    In [2]: Human.get_weight Out[2]: > 
我们看到get_weight是一个绑定在 Human 这个类上的method。
 In [3]: Human.get_weight() Out[3]: 12In [4]: Human().get_weight() Out[4]: 12 
类和类的实例都能调用 get_weight 而且调用结果完全一样。
我们看到 weight 是属于 Human 类的属性,当然也是 Human 的实例的属性。那传递过去的参数 cls 是类还是实例呢?
 In [1]: class Human(object):   ...:   weight = 12  ...:   @classmethod  ...:   def get_weight(cls):   ...:     print cls   In [2]: Human.get_weight()    In [3]: Human().get_weight()  
传递过去的都是 Human 类,不是 Human 的实例,两种方式调用的结果没有任何区别。cls 只是一个普通的函数参数,调用时被隐含的传递过去。
    classmethod 是类对象与函数的结合。 
    可以使用类和类的实例调用,但是都是将类作为codego.net的隐含参数传递过去。 
    使用类来调用 classmethod 可以避免将类实例化的开销。 
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.add Out[2]:    In [3]: Human().add Out[3]:    In [4]: Human.add(1, 2) Out[4]: 3   In [5]: Human().add(1, 2) Out[5]: 3 
 add 在无论是类还是实例上都只是一个普通的函数,并没有绑定在任何一个特定的类或者实例上。可以使用类或者类的实例调用,并且没有任何隐含参数的传入。
  In [6]: Human().add is Human().add Out[6]: True   In [7]: Human().get_weight is Human().get_weight Out[7]: False 

add 在两个实例上也是同一个对象。instancemethod 就不一样了,每次都会创建一个新的 get_weight 对象。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30187813/viewspace-1484732/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30187813/viewspace-1484732/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值