python bound unbound method
2014-03-24 10:38
246 查看
<function __main__.dd> assign function to class make it a unbound method of that class
eg: CLASSA.method1 = func1
Previously defined instances are updated as well (as long as they haven't overridden the attribute themselves):
but you can not add other class's method(unbound method) to a class
In [96]: A.a
Out[96]: <unbound method A.a>
In [97]: B.me = A.a
In [98]: B.me
Out[98]: <unbound method A.a> // you see!!! it is still A.a
// but we can make A.a func, then set this func to B
import types
t.update = types.MethodType(another_update)
the function type has a
special
which is called when you perform an attribute lookup on the class itself. Instead of you getting the function object, the
of that function object is called, and that returns a bound method object (which is what supplies the
In [185]: ddf = B.__dict__['ddf']
In [186]: ddf
Out[186]: <function __main__.dd>
In [187]: B.ddf
Out[187]: <unbound method B.dd>
http://codeplayer.iteye.com/blog/48144
eg: CLASSA.method1 = func1
>>> def fooFighters( self ): ... print "fooFighters" ... >>> A.fooFighters = fooFighters >>> a2 = A() >>> a2.fooFighters <bound method A.fooFighters of <__main__.A instance at 0x00A9BEB8>> >>> a2.fooFighters() fooFighters
Previously defined instances are updated as well (as long as they haven't overridden the attribute themselves):
but you can not add other class's method(unbound method) to a class
In [96]: A.a
Out[96]: <unbound method A.a>
In [97]: B.me = A.a
In [98]: B.me
Out[98]: <unbound method A.a> // you see!!! it is still A.a
// but we can make A.a func, then set this func to B
import types
t.update = types.MethodType(another_update)
the function type has a
special
__get__method
which is called when you perform an attribute lookup on the class itself. Instead of you getting the function object, the
__get__method
of that function object is called, and that returns a bound method object (which is what supplies the
selfargument).
In [185]: ddf = B.__dict__['ddf']
In [186]: ddf
Out[186]: <function __main__.dd>
In [187]: B.ddf
Out[187]: <unbound method B.dd>
http://codeplayer.iteye.com/blog/48144
相关文章推荐
- [Python] First-class Everything (Python缔造者Guido van Rossum关于bound/unbound method的来历叙述)
- python bound unbound method
- Class method differences in Python: bound, unbound and static
- python method unbound bound static区别和descriptor描述符解释
- function, method, unbound_method, bound_method
- centos6 点击input method preferences 没有反应--python版本造成
- Python中function和method
- Python: joblib.Parallel method in a class 'TypeError : can't pickle instancementhod objects'
- Python 实例方法、@staticmethod和@classmethod
- python -- @classmethod @staticmethod区别和使用
- python文件运行报错:UnboundLocalError: local variable 'status' referenced before assignment
- Python: classmethod & staticmethod 区别
- python dataframe (method,partial,dir,hasattr,setattr,getarrt)
- 飘逸的python - @staticmethod和@classmethod的作用与区别
- 【Python】UnboundLocalError: local variable 'l' referenced before assignment
- Python String translate() Method
- Python <built-in method items of dict object at 0x0000000002B6C8C8> 问题解决
- python:UnboundLocalError: local variable 'xxx' referenced before assignment
- python the method of super function
- python @ property classmethod staticmethod