I'm trying to override an object's next function using the code below (python 2.7).
When the object's next
method is called directly, the new function is invoked. However when I call the builtin next()
function on my object (which, according to the docs, should call the instance's next method), the ORIGINAL function is invoked.
Can someone explain this behaviour?
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