抽象方法未定义

发布于 2024-10-14 16:17:10 字数 1325 浏览 4 评论 0原文

我无法运行此代码,因为我得到了异常:

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__'

这不起作用?我没有进入 Debug I/0

I cannot run this code, because I get the exception:

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__()

I'm sure that code is ok, because I got it from http://docs.python.org/library/abc.html

EDIT

Thanks for answer, it works now, but why

print '__subclasshook__'

this doesn't work ? I don't get in Debug I/0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2024-10-21 16:17:10

您只导入了 ABCMeta

from abc import ABCMeta

还导入了 abstractmethod

from abc import ABCMeta, abstractmethod

,一切都应该没问题。

You only imported ABCMeta

from abc import ABCMeta

Also import abstractmethod

from abc import ABCMeta, abstractmethod

and everything should be fine.

最笨的告白 2024-10-21 16:17:10

您需要从 abc 导入 abstractmethodabc抽象基类。

一个例子:

from abc import abstractmethod

class Foo:
    def __init():
        pass
    
    @abstractmethod
    def bar():
        pass

You need to import abstractmethod from abc. abc is the inbuilt Python package for Abstract Base Classes.

An example:

from abc import abstractmethod

class Foo:
    def __init():
        pass
    
    @abstractmethod
    def bar():
        pass
分开我的手 2024-10-21 16:17:10

您需要将 import ABC 更改为 ABCMeta

from abc import ABCMeta, abstractmethod

class AbstractClassExample(ABCMeta):

    def __init__(self, value):
        self.value = value
        super().__init__()

    @abstractmethod
    def do_something(self):
        print("do_something")


class DoAdd42(AbstractClassExample):
    print("DoAdd42")

x = DoAdd42(4)

You need to change import ABC to ABCMeta

from abc import ABCMeta, abstractmethod

class AbstractClassExample(ABCMeta):

    def __init__(self, value):
        self.value = value
        super().__init__()

    @abstractmethod
    def do_something(self):
        print("do_something")


class DoAdd42(AbstractClassExample):
    print("DoAdd42")

x = DoAdd42(4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文