Python abstract class shall force derived classes to initialize variable in __init__
Edit: Solution with a custom metaclass.
It's worth noting that custom metaclasses are often frowned upon, but you can solve this problem with one.
Here is a good write up discussing how they work and when they're useful. The solution here is essentially to tack on a check for the attribute that you want after the __init__
is invoked.
from abc import ABCMeta, abstractmethod
# our version of ABCMeta with required attributes
class MyMeta(ABCMeta):
required_attributes = []
def __call__(self, *args, **kwargs):
obj = super(MyMeta, self).__call__(*args, **kwargs)
for attr_name in obj.required_attributes:
if not getattr(obj, attr_name):
raise ValueError('required attribute (%s) not set' % attr_name)
return obj
# similar to the above example, but inheriting MyMeta now
class Quadrature(object, metaclass=MyMeta):
required_attributes = ['xyz', 'weights']
@abstractmethod
def __init__(self, order):
pass
class QuadratureWhichWorks(Quadrature):
# This shall work because we initialize xyz and weights in __init__
def __init__(self,order):
self.xyz = 123
self.weights = 456
q = QuadratureWhichWorks('foo')
class QuadratureWhichShallNotWork(Quadrature):
def __init__(self, order):
self.xyz = 123
q2 = QuadratureWhichShallNotWork('bar')
Below is my original answer which explores the topic more in general.
Original Answer
I think some of this comes from confusing instance attributes with the objects wrapped by the property
decorator.
- An instance attribute is a plain chunk of data nested in the namespace of the instance. Likewise, a class attribute is nested in the namespace of the class (and shared by the instances of that class unless they overwrite it).
- A property is a function with syntactic shortcuts to make them accessible as if they were attributes, but their functional nature allows them to be dynamic.
A small example without introducing abstract classes would be
>>> class Joker(object):
>>> # a class attribute
>>> setup = 'Wenn ist das Nunstück git und Slotermeyer?'
>>>
>>> # a read-only property
>>> @property
>>> def warning(self):
>>> return 'Joke Warfare is explicitly banned bythe Geneva Conventions'
>>>
>>> def __init__(self):
>>> self.punchline = 'Ja! Beiherhund das Oder die Flipperwaldt gersput!'
>>> j = Joker()
>>> # we can access the class attribute via class or instance
>>> Joker.setup == j.setup
>>> # we can get the property but cannot set it
>>> j.warning
'Joke Warfare is explicitly banned bythe Geneva Conventions'
>>> j.warning = 'Totally safe joke...'
AttributeError: cant set attribute
>>> # instance attribute set in __init__ is only accessible to that instance
>>> j.punchline != Joker.punchline
AttributeError: type object 'Joker' has no attribute 'punchline'
According to the Python docs, since 3.3 the abstractproperty
is redundant and actually reflects your attempted solution.
The issue with that solution is that your subclasses do not implement a concrete property, they just overwrite it with an instance attribute.
In order to continue using the abc
package, you could handle this by implementing those properties, i.e.
>>> from abc import ABCMeta, abstractmethod
>>> class Quadrature(object, metaclass=ABCMeta):
>>>
>>> @property
>>> @abstractmethod
>>> def xyz(self):
>>> pass
>>>
>>> @property
>>> @abstractmethod
>>> def weights(self):
>>> pass
>>>
>>> @abstractmethod
>>> def __init__(self, order):
>>> pass
>>>
>>> def someStupidFunctionDefinedHere(self, n):
>>> return self.xyz+self.weights+n
>>>
>>>
>>> class QuadratureWhichWorks(Quadrature):
>>> # This shall work because we initialize xyz and weights in __init__
>>> def __init__(self,order):
>>> self._xyz = 123
>>> self._weights = 456
>>>
>>> @property
>>> def xyz(self):
>>> return self._xyz
>>>
>>> @property
>>> def weights(self):
>>> return self._weights
>>>
>>> q = QuadratureWhichWorks('foo')
>>> q.xyz
123
>>> q.weights
456
I think this is a bit clunky though, but it really depends on how you intend to implment subclasses of Quadrature
.
My suggestion would be to not make xyz
or weights
abstract, but instead handle whether they were set at runtime, i.e. catch any AttributeError
s that may pop up when accessing the value.
In order to force a subclass to implement a property or method, you need to raise an error, if this method is not implemented:
from abc import ABCMeta, abstractmethod, abstractproperty
class Quadrature(object, metaclass=ABCMeta):
@abstractproperty
def xyz(self):
raise NotImplementedError
Class Annotations Solution
This is possible because of changes to python 3.7 (which I hope you are using - because this is cool!) as it adds type hinting
and the ability to add class annotations, which were added for dataclasses
. It is as close to your original desired syntax as I can think up. The superclass you will want would look something like this:
from abc import ABC, abstractmethod
from typing import List
class PropertyEnfocedABC(ABC):
def __init__(self):
annotations = self.__class__.__dict__.get('__annotations__', {})
for name, type_ in annotations.items():
if not hasattr(self, name):
raise AttributeError(f'required attribute {name} not present '
f'in {self.__class__}')
Now, to see it in action.
class Quadratic(PropertyEnfocedABC):
xyz: int
weights: List[int]
def __init__(self):
self.xyz = 2
self.weights = [4]
super().__init__()
or more accurately in your case, with a mix of abstract methods and attributes:
class Quadrature(PropertyEnforcedABC):
xyz: int
weights: int
@abstractmethod
def __init__(self, order):
pass
@abstractmethod
def some_stupid_function(self, n):
return self.xyz + self.weights + n
Now, any subclass of a subclass of a PropertyEnforcedABC
must set the attributes that are annotated in the class (if you do not provide a type to the annotation it will not be considered an annotation) and thus if the constructor of quadratic did not set xyz
or weights
, an attribute error would be raised. Note you must call the constructor at the end of init, but this should not be a real problem and you can solve this by wrapping your own metaclass around the above code if you really don't like it.
You could modify PropertyEnforcedABC
however you want (like enforcing the type of the properties) and more. You could even check for Optional
and ignore those.