Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract attributes in Python [duplicate]

What is the shortest / most elegant way to implement the following Scala code with an abstract attribute in Python?

abstract class Controller {      val path: String  } 

A subclass of Controller is enforced to define "path" by the Scala compiler. A subclass would look like this:

class MyController extends Controller {      override val path = "/home"  } 
like image 962
deamon Avatar asked Apr 29 '10 09:04

deamon


1 Answers

Python 3.3+

from abc import ABCMeta, abstractmethod   class A(metaclass=ABCMeta):     def __init__(self):         # ...         pass      @property     @abstractmethod     def a(self):         pass      @abstractmethod     def b(self):         pass   class B(A):     a = 1      def b(self):         pass 

Failure to declare a or b in the derived class B will raise a TypeError such as:

TypeError: Can't instantiate abstract class B with abstract methods a

Python 2.7

There is an @abstractproperty decorator for this:

from abc import ABCMeta, abstractmethod, abstractproperty   class A:     __metaclass__ = ABCMeta      def __init__(self):         # ...         pass      @abstractproperty     def a(self):         pass      @abstractmethod     def b(self):         pass   class B(A):     a = 1      def b(self):         pass 
like image 90
Wtower Avatar answered Nov 08 '22 10:11

Wtower