【发布时间】:2017-01-05 15:03:53
【问题描述】:
我正在尝试使用下面的代码 (python 2.7) 覆盖对象的下一个函数。
直接调用对象的next方法时,会调用新函数。然而,当我在我的对象上调用内置的 next() 函数(according to the docs 应该调用实例的 next 方法)时,会调用 ORIGINAL 函数。
有人可以解释这种行为吗?
class Test(object):
def __iter__(self):
return self
def next(self):
return 1
test = Test()
def new_next(self):
return 2
test.next = type(test.__class__.next)(new_next, test, test.__class__)
print test.next() # 2
print next(test) # 1
【问题讨论】:
-
有趣。可能与PEP 3114有关?
-
@AndréLaszlo 看起来 PEP 适用于 Python 3
-
@warvariuc 哎呀,你是对的。
标签: python