-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathabstract.py
50 lines (38 loc) · 1.23 KB
/
abstract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Demonstrates how to combine Struct with abstract base classes."""
from abc import ABCMeta, abstractmethod
from simplestruct import Struct, Field, MetaStruct
# A simple ABC. Subclasses must provide an override for foo().
class Abstract(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
# ABCs rely on a metaclass that conflicts with Struct's metaclass.
try:
class Concrete(Abstract, Struct):
f = Field
def foo(self):
return self.f ** 2
except TypeError as e:
print(e)
# metaclass conflict: the metaclass of a derived class must
# be a (non-strict) subclass of the metaclasses of all its bases
# So let's make a trivial subclass of ABCMeta and MetaStruct.
class ABCMetaStruct(MetaStruct, ABCMeta):
pass
class Concrete(Abstract, Struct, metaclass=ABCMetaStruct):
f = Field
def foo(self):
return self.f ** 2
c = Concrete(5)
print(c.foo()) # 25
# For convenience we can make a version of Struct that
# incorporates the common metaclass.
class ABCStruct(Struct, metaclass=ABCMetaStruct):
pass
# Now we only have to do:
class Concrete(Abstract, ABCStruct):
f = Field
def foo(self):
return self.f ** 2
c = Concrete(5)
print(c.foo()) # 25