python – 当类中嵌入的函数是类的“方法”时?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 当类中嵌入的函数是类的“方法”时?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2720字,纯文字阅读大概需要4分钟。
内容图文
![python – 当类中嵌入的函数是类的“方法”时?](/upload/InfoBanner/zyjiaocheng/821/d70dfa4ff1ad4c0ebab40f41ffdb4d5b.jpg)
执行此操作时……
class A:
def b(self, a):
print a
print dir(b)
print dir(A.b)
它给出了结果:
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure',
'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self']
为什么这个不同?
解决方法:
在Python 2中,第二种情况返回一个未绑定的方法,第一种情况是一个函数.从the documentation开始,强调我的:
User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is.
所以你得到的方法纯粹是因为A.前缀.原始函数仍然可以通过方法的im_func(或从2.6开始也__func__)属性访问.另请注意,每次访问都会获得一个新的方法对象,因此A.b为A.b,返回False!
在Python 3中,未绑定的方法不再作为单独的类型存在,并且您将从两个打印调用获得相同的输出:
$python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def b(self, a):
... print(a)
... print(dir(b))
...
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> print(dir(A.b))
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
如果你用id替换dir,你会发现它在两种情况下都是完全相同的函数对象,在Python 3中.
内容总结
以上是互联网集市为您收集整理的python – 当类中嵌入的函数是类的“方法”时?全部内容,希望文章能够帮你解决python – 当类中嵌入的函数是类的“方法”时?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。