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]: <unbound method Human.get_weight>
In [7]: Human.get_weight() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/yao/learn/insight_python/<ipython-input-7-a2b2c5cd2f8d> in <module>() ----> 1 Human.get_weight() TypeError: unbound method get_weight() must be called with Human instance as first argument (got nothing instead)
In [10]: Human.get_weight(Human(45)) Out[10]: 45
In [11]: person = Human(45) In [12]: person.get_weight() Out[12]: 45
In [13]: person.get_weight Out[13]: <bound method Human.get_weight of <__main__.Human object at 0x8e13bec>> In [14]: person Out[14]: <__main__.Human at 0x8e13bec>
In [1]: class Human(object): ...: weight = 12 ...: @classmethod ...: def get_weight(cls): ...: return cls.weight In [2]: Human.get_weight Out[2]: <bound method type.get_weight of <class '__main__.Human'>>
In [3]: Human.get_weight() Out[3]: 12 In [4]: Human().get_weight() Out[4]: 12
In [1]: class Human(object): ...: weight = 12 ...: @classmethod ...: def get_weight(cls): ...: print cls In [2]: Human.get_weight() <class '__main__.Human'> In [3]: Human().get_weight() <class '__main__.Human'>
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]: <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
In [6]: Human().add is Human().add Out[6]: True In [7]: Human().get_weight is Human().get_weight Out[7]: False
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有