Abstract class and interface definitions
Project description
Abstract class and interface definitions.
Create an abstract.Abstraction
An Abstraction is a metaclass for defining abstract classes.
Let’s define an abstract AFoo class and give it an abstract do_foo method.
Like any python class, an Abstraction can have any name, but it may be helpful to distinguish abstract classes from others by prefixing their name with A.
>>> import abc
>>> import abstracts
>>> class AFoo(metaclass=abstracts.Abstraction):
...
... @abc.abstractmethod
... def do_foo(self):
... raise NotImplementedError
Abstract classes cannot be instantiated directly.
>>> AFoo()
Traceback (most recent call last):
...
TypeError: Can't instantiate abstract class AFoo with abstract method... do_foo
Create an implementer for an abstract.Abstraction
In order to make use of AFoo, we need to create an implementer for it.
>>> @abstracts.implementer(AFoo)
... class Foo:
... pass
The implementer must implement all of the abstract methods, defined by its abstract classes.
>>> Foo()
Traceback (most recent call last):
...
TypeError: Can't instantiate abstract class Foo with abstract method... do_foo
>>> @abstracts.implementer(AFoo)
... class Foo2:
...
... def do_foo(self):
... return "DID FOO"
>>> Foo2()
<__main__.Foo2 object at ...>
An implementer inherits from its Abstractions
An implementer class is a subclass of its Abstraction.
>>> issubclass(Foo2, AFoo)
True
Likewise an instance of an implementer is an instance of its Abstraction
>>> isinstance(Foo2(), AFoo)
True
The Abstraction class can be seen in the class bases, and the methods of the Abstraction can be invoked by the implementer.
>>> import inspect
>>> AFoo in inspect.getmro(Foo2)
True
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for abstracts-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b45fe0deae5dd9e6b0fd15f1e9f4654c89e700fcf0f8248a17526551891beea |
|
MD5 | 845277ade7a10748480c6c9930c4610e |
|
BLAKE2b-256 | f4727a3e5fb1418c411c5959bce9a1cc9b07606cbcc6a9c46285efc8824974dc |