举个例子:
>>> class Cool:
... def mthd(self, val):
... self.val = val
...
>>> def x(): pass
>>> Cool.mthd(x, 360) # Works in 3.X only, not in 2.X
2.X 引发 **TypeError: unbound method...** 错误,但在 3.x 中它工作得很好。
Why does Python 2.X restricts self argument to be only an instance type, whereas Python 3.X doesn't restrict it to any data type? And why was this changed in 3.X?
请您参考如下方法:
我不知道为什么它被改变了(我猜测删除了一个没有增加很多值(value)的概念),但是这个变化记录在in the 'What’s New In Python 3.0' documentation :
The concept of “unbound methods” has been removed from the language. When referencing a method as a class attribute, you now get a plain function object.
有了这个改变,
my_cool.mthd(360)
只是成为(非常推荐!)的简写符号
type(my_cool).mthd(my_cool, 360)
