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. MIT License
Copyright (c) 2020 Tim Fischer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
Built Distribution
File details
Details for the file pycompose-1.2.tar.gz
.
File metadata
- Download URL: pycompose-1.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6287e25d124982bebd60366476c344ea4fbc9219bae5642a77b492082f4424c6 |
|
MD5 | 66fe1eb541fd78763afc40a774de9be7 |
|
BLAKE2b-256 | aa29f9d353ddc291ee8fa9c4036b7adeaca9896fa3e86df3b24066754be72151 |
File details
Details for the file pycompose-1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: pycompose-1.2-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe36a6cab701aad8f46b4918c285a3f893d0104a645408231a92f1cb796347a1 |
|
MD5 | c53075591a1aa9212db19f7871ebfb56 |
|
BLAKE2b-256 | 9a37a0b980fe19a72e3e10958176eecea773e89a76b557717aa6f8a66b0ed662 |