Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python @abstractmethod decorator

I have read python docs about abstract base classes:

From here:

abc.abstractmethod(function) A decorator indicating abstract methods.

Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden.

And here

You can apply the @abstractmethod decorator to methods such as draw() that must be implemented; Python will then raise an exception for classes that don’t define the method. Note that the exception is only raised when you actually try to create an instance of a subclass lacking the method.

I've used this code to test that out:

import abc  class AbstractClass(object):   __metaclass__ = abc.ABCMeta    @abc.abstractmethod   def abstractMethod(self):     return  class ConcreteClass(AbstractClass):   def __init__(self):     self.me = "me"  c = ConcreteClass() c.abstractMethod() 

The code goes fine, so I don't get it. If I type c.abstractMethod I get:

<bound method ConcreteClass.abstractMethod of <__main__.ConcreteClass object at 0x7f694da1c3d0>> 

What I'm missing here? ConcreteClass must implement the abstract methods, but I get no exception.

like image 785
Sebastian Avatar asked Aug 25 '11 19:08

Sebastian


People also ask

What is @abstractmethod in Python?

An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and its abstract methods must be implemented by its subclasses.

What is Abstractproperty Python?

Abstract Properties Properties are Pythonic ways of using getters and setters. The abc module has a @abstractproperty decorator to use abstract properties. Just like abstract methods, we need to define abstract properties in implementation classes, otherwise, Python will raise the error.

What is ABC module in Python?

Python has a module called abc (abstract base class) that offers the necessary tools for crafting an abstract base class. First and foremost, you should understand the ABCMeta metaclass provided by the abstract base class. The rule is every abstract class must use ABCMeta metaclass.

Does Python have abstract classes?

Python doesn't directly support abstract classes. But it does offer a module that allows you to define abstract classes. To define an abstract class, you use the abc (abstract base class) module. The abc module provides you with the infrastructure for defining abstract base classes.


2 Answers

Are you using python3 to run that code? If yes, you should know that declaring metaclass in python3 have changes you should do it like this instead:

import abc  class AbstractClass(metaclass=abc.ABCMeta):    @abc.abstractmethod   def abstractMethod(self):       return 

The full code and the explanation behind the answer is:

import abc  class AbstractClass(metaclass=abc.ABCMeta):      @abc.abstractmethod     def abstractMethod(self):         return  class ConcreteClass(AbstractClass):      def __init__(self):         self.me = "me"  # Will get a TypeError without the following two lines: #   def abstractMethod(self): #       return 0  c = ConcreteClass() c.abstractMethod() 

If abstractMethod is not defined for ConcreteClass, the following exception will be raised when running the above code: TypeError: Can't instantiate abstract class ConcreteClass with abstract methods abstractMethod

like image 154
mouad Avatar answered Sep 25 '22 19:09

mouad


Import ABC from abc and make your own abstract class a child of ABC can help make the code look cleaner.

from abc import ABC, abstractmethod  class AbstractClass(ABC):    @abstractmethod   def abstractMethod(self):     return  class ConcreteClass(AbstractClass):   def __init__(self):     self.me = "me"  # The following would raise a TypeError complaining  # abstractMethod is not implemented c = ConcreteClass()   

Tested with Python 3.6

like image 29
l001d Avatar answered Sep 25 '22 19:09

l001d