Skip to main content

Prefer composition over inheritance

Project description

Compclasses

Code style: black

Like dataclasses, but for composition.

As the Gang of Four (probably) said:

favor object composition over class inheritance.

However when we use composition in Python, we cannot access methods directly from the composed class, and we either re-define these methods from scratch, or access them using chaining.

This codebase wants to address such issue and make it easy to do so, by delegating the desired methods of a class to its attributes.


Documentation: https://fbruzzesi.github.io/compclasses

Source Code: https://github.com/fbruzzesi/compclasses


Table of Content

Alpha Notice

This codebase is experimental and is working for my use cases. It is very probable that there are cases not covered and for which everything breaks. If you find them, please feel free to open an issue in the issue page of the repo.

Installation

compclasses is published as a Python package on pypi, and it can be installed with pip, ideally by using a virtual environment.

From a terminal it is possible to install it with:

python -m pip install compclasses

Getting Started

Let's suppose we have the following 3 classes, Foo, Bar and Baz:

  • Foo and Bar are independent from one another;
  • Baz get initialized with two class attributes (_foo, _bar) which are instances of the other two classes.
class Foo:
    """Foo class"""

    def __init__(self, value: int):
        """foo init"""
        self._value = value

    def get_value(self):
        """get value attribute"""
        return self._value

    def hello(self, name: str) -> str:
        """Method with argument"""
        return f"Hello {name}, this is Foo!"


class Bar:
    """Bar class"""
    b: int = 1

    def __len__(self) -> int:
        """Custom len method"""
        return 42

class Baz:
    """Baz class"""

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

Now let's instantiate them and try see how we would access the "inner" attributes/methods:

foo = Foo(123)
bar = Bar()

baz = Baz(foo, bar)

baz._foo.get_value()  # -> 123
baz._foo.hello("GitHub")  # -> "Hello GitHub, this is Foo!"
baz._bar.__len__()  # -> 42

len(baz)  # -> TypeError: object of type 'Baz' has no len()

compclass (decorator)

Using the compclass decorator we can forward the methods that we want to the Baz class from its attributes at definition time:

from compclasses import compclass

delegates = {
    "_foo": ( "get_value", "hello"),
    "_bar": ("__len__", )
}

@compclass(delegates=delegates)
class Baz:
    """Baz class"""

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

baz = Baz(foo, bar)
baz.get_value()  # -> 123
baz.hello("GitHub")  # -> "Hello GitHub, this is Foo!"
len(baz)  # -> 42

We can see how now we can access the methods directly.

Remark that in the delegates dictionary, we have that:

  • the keys correspond to the attribute names in the Baz class;
  • each value should be an iterable of string corresponding to methods/attributes present in the class instance associated to the key-attribute.

The compclass decorator adds each attribute and method as a property attribute, callable as self.<attr_name> instead of self.<delegatee_cls>.<attr_name>

CompclassMeta (metaclass)

The equivalent, but alternative, way of doing it is by using the CompclassMeta metaclass when you define the class.

from compclasses import CompclassMeta

delegates = {
    "_foo": ( "get_value", "hello"),
    "_bar": ("__len__", )
}

class Baz(metaclass=CompclassMeta, delegates=delegates):
    """Baz class"""

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

baz = Baz(foo, bar)
baz.get_value()  # -> 123
baz.hello("GitHub")  # -> "Hello GitHub, this is Foo!"
len(baz)  # -> 42

As you can see the syntax is nearly one-to-one with the compclass decorator, and the resulting behaviour is exactly the same!

Advanced usage

Instead of using an iterable in the delegates dictionary, we suggest to use a delegatee instance as a value.

This will yield more flexibility and features when decide to forward class attributes and methods.

Check the dedicated documentation page to get a better understanding and see more examples on how delegatee can be used, its pros and cons.

Why Composition (TL;DR)

Overall, composition is a more flexible and transparent way to reuse code and design classes in Python. It allows to build classes that are customized to your specific needs, and it promotes code reuse and modularity.

A more detailed explaination is present in the documentation page.

Feedbacks

Any feedback, improvement/enhancement or issue is welcome in the issue page of the repo.

Contributing

Please refer to the contributing guidelines in the documentation site.

Inspiration

This projects is inspired by the forwardable library, a "utility for easy object composition via delegation".

However I was looking for both more flexibility and more features. In particular:

  • a clear separation between class definition and method forwarding;
  • a validation step to make sure that changing something from the component doesn't break the class;
  • the possibility to forward all the methods/attributes of a given component with a single instruction;
  • the chance of adding prefix and/or suffix for each component;

Please refer to Beyond the basics page to see example usages.

Licence

The project has a MIT Licence

Project details


Download files

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

Source Distribution

compclasses-0.4.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

compclasses-0.4.0-py2.py3-none-any.whl (11.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file compclasses-0.4.0.tar.gz.

File metadata

  • Download URL: compclasses-0.4.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.28.2 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.8.5

File hashes

Hashes for compclasses-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4725f0a71b103060de1ef327a9f0e06832716507f0219574cbfe20df603ea173
MD5 739ddb4904b70d393872c4255ad077d1
BLAKE2b-256 ef174bad7f2c56cc6a3d01ccdeb35bb72b68920c49ad080ccde75d438653e76f

See more details on using hashes here.

File details

Details for the file compclasses-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: compclasses-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.28.2 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.8.5

File hashes

Hashes for compclasses-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a1cf01c8b576f0f0b674b7a15bc619bb32a320411dcb8143a998267d551f2096
MD5 94fccd9118f9be7f8a2299ec040fa2a6
BLAKE2b-256 da9206f35fda2964931c3625e0b619c2cbf313331280bd2e3a8df0e5c21cf8c0

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