Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstractmethod is not defined

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

like image 424
user278618 Avatar asked Jan 27 '11 09:01

user278618


People also ask

What is Abstractmethod in Python?

An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class.

What's Abstractmethod?

An abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods.

Can abstract methods have implementation Python?

This impression is wrong: An abstract method can have an implementation in the abstract class! Even if they are implemented, designers of subclasses will be forced to override the implementation. Like in other cases of "normal" inheritance, the abstract method can be invoked with super() call mechanism.

How do you instantiate an abstract class in Python?

Abstract base classes cannot be instantiated. Instead, they are inherited and extended by the concrete subclasses. Subclasses derived from a specific abstract base class must implement the methods and properties provided in that abstract base class. Otherwise, an error is raised during the object instantiation.


2 Answers

You only imported ABCMeta

from abc import ABCMeta

Also import abstractmethod

from abc import ABCMeta, abstractmethod

and everything should be fine.

like image 184
Sven Marnach Avatar answered Oct 02 '22 06:10

Sven Marnach


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
like image 40
gruszczy Avatar answered Oct 02 '22 05:10

gruszczy