Skip to main content

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

Create an implementer that implements multiple Abstraction s.

An implementer can implement multiple abstractions.

Let’s create a second abstraction.

>>> class ABar(metaclass=abstracts.Abstraction):
...
...     @abc.abstractmethod
...     def do_bar(self):
...         raise NotImplementedError

And now we can create an implementer that implememts both the AFoo and ABar Abstraction s.

>>> @abstracts.implementer((AFoo, ABar))
... class FooBar:
...
...     def do_foo(self):
...         return "DID FOO"
...
...     def do_bar(self):
...         return "DID BAR"

>>> FooBar()
<__main__.FooBar object at ...>

Defining abstract properties

Properties can be defined in an abstract class, and just like with normal methods, they must be implemented by any implementers.

>>> class AMover(metaclass=abstracts.Abstraction):
...
...     @property
...     @abc.abstractmethod
...     def speed(self):
...         return 5
...
...     @property
...     @abc.abstractmethod
...     def direction(self):
...         return "forwards"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

abstracts-0.0.5-py3-none-any.whl (100.1 kB view details)

Uploaded Python 3

File details

Details for the file abstracts-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: abstracts-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 100.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.2

File hashes

Hashes for abstracts-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b2d0bd41cae48ea56d52a6a5a99659098b275577c06f7f3496705eab4017ef6a
MD5 0e7488e14f04e350738481888205a7bf
BLAKE2b-256 601ca36bf38942facab56cbc04db3bfe23b658b92187ba2184b8dec4e042a441

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page