how to define an abstract class in python and force implement variables

frazman

So, I am trying to define an abstract base class with couple of variables which I want to to make it mandatory to have for any class which "inherits" this base class.. So, something like:

class AbstractBaseClass(object):
   foo = NotImplemented
   bar = NotImplemented

Now,

class ConcreteClass(AbstractBaseClass):
    # here I want the developer to force create the class variables foo and bar:
    def __init__(self...):
        self.foo = 'foo'
        self.bar = 'bar'

This should throw error:

class ConcreteClass(AbstractBaseClass):
    # here I want the developer to force create the class variables foo and bar:
    def __init__(self...):
        self.foo = 'foo'
        #error because bar is missing??

I maybe using the wrong terminology.. but basically, I want every developer who is "implementing" the above class to force to define these variables??

sirfz

Update: abc.abstractproperty has been deprecated in Python 3.3. Use property with abc.abstractmethod instead as shown here.

import abc

class AbstractBaseClass(object):

    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def foo(self):
        pass

    @abc.abstractproperty
    def bar(self):
        pass

class ConcreteClass(AbstractBaseClass):

    def __init__(self, foo, bar):
        self._foo = foo
        self._bar = bar

    @property
    def foo(self):
        return self._foo

    @foo.setter
    def foo(self, value):
        self._foo = value

    @property
    def bar(self):
        return self._bar

    @bar.setter
    def bar(self, value):
        self._bar = value

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python3 - How does one define an abstract subclass from an existing abstract class?

How define constructor implementation for an Abstract Class in Python?

How define constructor implementation for an Abstract Class in Python?

How to implement an abstract class in Go?

Is there a way to force a derived class to implement an abstract class or an interface nested in base class?

Nested abstract class in an abstract class and how to implement it

How to implement generic fields from abstract class?

In TypeScript how to force inherited class to implement a method

How to force descendant classes to implement an abstract function?

How to implement an abstract method when abstract class is used in a variadic context

How to define "in" for a python class

Exercise: Implement an abstract class

How to define variables that will be used in every iteration of a function within a class in python?

How to define a generic abstract class that inherits a generic abstract class?

How to implement Dependencies Injection in abstract class

How to force a class that implements an abstract class to declare its variables?

Java how to implement and design an abstract class

How to implement methods of an abstract class? (Java)

Implement IDisposable in abstract class

How to Implement Extension Method in Abstract Class

How to implement Interface to an abstract class which is constraint?

How to implement Abstract class in laravel 5.3

How to implement operator+ in abstract base class

Using Python class name to define class variables

How to force sub class to implement a static method

How to define an abstract metaclass in python

How to implement abstract class in Kotlin

How to define methods and member variables in class defined with custom metaclass in python

How to define Iterators for an abstract class in C++