“unbound method”的含义在 Python2 和 Python3 中是否已经改变 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
u2386
V2EX    Python

“unbound method”的含义在 Python2 和 Python3 中是否已经改变

  •  
  •   u2386 2018-05-06 16:17:30 +08:00 3299 次点击
    这是一个创建于 2796 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是通过 inspect 库获取类内方法的两端代码:

    Python2

    >>> class A(object): ... def a(self): ... print('a') ... ... @staticmethod ... def b(): ... print('b') ... ... @classmethod ... def c(cls): ... print('c') ... >>> import inspect >>> inspect.getmembers(A, inspect.ismethod) [('a', <unbound method A.a>), ('c', <bound method type.c of <class '__main__.A'>>)] 

    Python3

    >>> class A(object): ... def a(self): ... print('a') ... ... @staticmethod ... def b(): ... print('b') ... ... @classmethod ... def c(cls): ... print('c') ... >>> import inspect >>> inspect.getmembers(A, inspect.ismethod) [('c', <bound method A.c of <class '__main__.A'>>)] 

    可以看到对于类而言,实例方法在 Python3 中已经不再是method,而是function

    >>> inspect.getmembers(A, inspect.isfunction) [('a', <function A.a at 0x10d46e598>), ('b', <function A.b at 0x10d46e620>)] 

    通过查阅两个版本的 inspect 文档可以看到在Python2中:

    inspect.ismethod(object)

    Return true if the object is a bound or unbound method written in Python.

    相对于在Python3中:

    inspect.ismethod(object)

    Return true if the object is a bound method written in Python.

    ismethod不在包含 unbound method 了。

    这是否是 Python2 到 Python3 后的通识区别?可惜这么重要的区别并没有被大多数的 “ Differences between Python2 and Python3 ” 之类的文章提到。

    6 条回复    2018-05-10 18:02:24 +08:00
    locktionc
        1
    locktionc  
       2018-05-06 16:27:28 +08:00
    你这个发现非常好。
    chenxytw
        2
    chenxytw  
       2018-05-06 16:33:10 +08:00
    不是通识区别.....
    因为这个已经是比较高级的应用了 0 0 不是那种所有程序员都必须懂的知识......

    可以看这封 dev 邮件
    https://mail.python.org/pipermail/python-dev/2009-February/086233.html
    简言之, unbound method 其实在 Python3 中已经遗弃了,没有这种概念了
    chenxytw
        3
    chenxytw  
       2018-05-06 16:44:11 +08:00   1
    不过这个概念的完全移除弄了好久。。。。
    可以看这个 bug
    https://bugs.python.org/issue23702
    u2386
        4
    u2386  
    OP
       2018-05-06 16:50:55 +08:00
    @chenxytw 也是项目中的一个“骚操作”让我踩到了♂

    虽然不是“通识”,但这个改变毕竟不是简单的语法改变,而是概念上变动,我觉得应该有一个官方的提醒。
    变动不比 print 从 statement 变成 function 来的小。
    chenxytw
        5
    chenxytw  
       2018-05-06 17:04:48 +08:00
    @u2386 可能在他们看来,这个不影响大多数程序的运行吧 Orz
    a132811
        6
    a132811  
       2018-05-10 18:02:24 +08:00
    python3 这样更加清晰了.
    @staticmethod 本来就是纯函数,不用 self/cls
    @classmethod 是 method, 默认带 cls
    A.a 也是纯函数
    A().a 才是 method, 默认带 cls, 这里的 cls=A() 其实就是 self

    cls 本质就是 metaclass 的实例(实例自己叫自己 self )
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2354 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 03:05 PVG 11:05 LAX 19:05 JFK 22:05
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86