我想弄明白爲什麼下面的例子不起作用。重寫猴子修補的方法
class BaseClass(object):
def __init__(self):
self.count = 1
def __iter__(self):
return self
def next(self):
if self.count:
self.count -= 1
return self
else:
raise StopIteration
class DerivedNO(BaseClass):
pass
class DerivedO(BaseClass):
def __init__(self):
self.new_count = 2
self.next = self.new_next
def new_next(self):
if self.new_count:
self.new_count -= 1
return None
else:
raise StopIteration
x = DerivedNO()
y = DerivedO()
print x
print list(x)
print y
print list(y)
這裏是輸出:
<__main__.DerivedNO object at 0x7fb2af7d1c90>
[<__main__.DerivedNO object at 0x7fb2af7d1c90>]
<__main__.DerivedO object at 0x7fb2af7d1d10>
Traceback (most recent call last):
File "playground.py", line 41, in <module>
print list(y)
File "playground.py", line 11, in next
if self.count:
AttributeError: 'DerivedO' object has no attribute 'count'
正如你可以看到新的方法不會在DerivedO
被覆蓋,當我嘗試在__init__
分配next()
方法。這是爲什麼?對下一個簡單的調用會很好,但在使用迭代技術時根本不會。
編輯:我意識到我的問題並不完全清楚。 AttributeError不是我想要解決的問題。但它確實顯示next()
在BaseClass
上被調用,而不是在我想象的DerivedO
上。
我想你只需要在派生類的構造函數中調用基類「__init__」。 – sje397 2012-07-27 12:23:45
@ sje397:這就是要點; 'new_next'方法不會引用'count'。 – 2012-07-27 12:29:22
有趣的是,當從'__iter__'返回'self'時,使用類定義的'next',不管在實例上是否設置了next。 '__iter__'方法同樣適用,使用'yield'方法修補它也被忽略。 – 2012-07-27 12:34:20