Skip to main content

A simple way to inject composition through inheritance syntax.

Project description

PyCompose

A simple way to inject composition through inheritance syntax.

Inspirations

The following links served as inpiration during the writing of this, and as a in-depth explanation of what actually is composition and its benefits and drawbacks versus inheritance.

Explanation

From the inspirations one can easily see that an older implementation of compositional forwarding exists (forwardable) so why would I do this to myself? In short, I didn't like the syntax introduced by it. I find the syntax provided by this implementation a bit easier to understand as I find adding things that related to properties of a class inside the brackets in the class statement the most logical.

Usage

from pycompose import Compose


class A:
    test1 = "123"

    def hello(self):
        print("A")

    def __repr__(self):
        return "<A>"


class B:
    test2 = "456"

    def hello(self):
        print("B")

    def __repr__(self):
        return "<B>"


class C(
    Compose(A, "test1", ("hello", "helloA")),
    Compose(B, "test2", ("hello", "helloB"), name="b_object"),
):
    ...


c = C()
print(c.test1 + c.test2)  # > 123456
c.helloA()                # > A
c.helloB()                # > B
print(c._a, c.b_object)   # > <A> <B>

Disclaimer

Please do note that this project is but a small abstraction for something that can easily be achieved only a few more lines of hand written code. As such I am aware of the fact that this may seem pretty useless to some people.

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

pycompose-1.1.linux-x86_64.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

pycompose-1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pycompose-1.1.linux-x86_64.tar.gz.

File metadata

  • Download URL: pycompose-1.1.linux-x86_64.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pycompose-1.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 ed30c044c8fca6918354c0c9e1f2dd5df76bb0c945c1f9bf8f212515b0344ad9
MD5 cd5f04091c0b05f9d204fbf1e4f70268
BLAKE2b-256 2bfafb7213c198b2fb4b1b7212cf5bea7638ef7373829533d2d3788644599add

See more details on using hashes here.

File details

Details for the file pycompose-1.1-py3-none-any.whl.

File metadata

  • Download URL: pycompose-1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pycompose-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8162facc077c1a19e6c68a8b8d3747a345f6c90ef4a633a43e43a5fc4d9f0b85
MD5 0d35db156f1ddd0f468d1bad55918011
BLAKE2b-256 b13f577dd994fb3cf67377c64d7805d51824c3b0383a2fd00f6cbb9347cb5285

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