मैं इस कोड नहीं चलाया जा सकता, क्योंकि मैं अपवाद:abstractmethod परिभाषित नहीं है
NameError: name 'abstractmethod' is not defined
File "C:\Tests\trunk\PythonTests\AbstractClasses.py", line 12, in <module>
class MyIterable:
File "C:\Tests\trunk\PythonTests\AbstractClasses.py", line 15, in MyIterable
@abstractmethod
from abc import ABCMeta
class Foo(object):
def __getitem__(self, index):
print '__get_item__ Foo'
def __len__(self):
print '__len__ Foo'
def get_iterator(self):
print 'get_iterator Foo'
return iter(self)
class MyIterable:
__metaclass__ = ABCMeta
@abstractmethod
def __iter__(self):
while False:
yield None
def get_iterator(self):
return self.__iter__()
@classmethod
def __subclasshook__(cls, C):
if cls is MyIterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
print "I'm in __subclasshook__"
return True
return NotImplemented
MyIterable.register(Foo)
x=Foo()
x.__subclasshook__()
मुझे यकीन है कि कोड ठीक है कर रहा हूँ क्योंकि मैं इसे http://docs.python.org/library/abc.html
संपादित
से मिलाउत्तर के लिए धन्यवाद, यह अब काम करता है, लेकिन क्यों
print '__subclasshook__'
यह काम नहीं करता है? मैं डीबग मैं/0
क्यों 'प्रिंट' मैं __subclasshook __ 'में काम नहीं करता हूं? क्या आपने वास्तव में उद्धरण वर्णों '' 'की गणना करने के लिए समय निकाला है? –
क्षमा करें, वास्तव में '__subclasshook__' है। मैंने यहां स्पष्टता के लिए एक और पाठ लिखा: / – user278618