https://github.com/lukashedegaard/supers
Call a function in all superclasses using `supers(self).foo(42)`
https://github.com/lukashedegaard/supers
Last synced: 2 months ago
JSON representation
Call a function in all superclasses using `supers(self).foo(42)`
- Host: GitHub
- URL: https://github.com/lukashedegaard/supers
- Owner: LukasHedegaard
- License: mit
- Created: 2020-07-29T12:42:18.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-15T20:30:02.000Z (over 1 year ago)
- Last Synced: 2025-02-07T00:46:31.943Z (3 months ago)
- Language: Python
- Size: 22.5 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π¦ΈββοΈ Supers: Call a function in all superclasses as easy as `supers(self).foo(42)`

[](https://codecov.io/gh/LukasHedegaard/datasetops)
[](https://github.com/psf/black)## Installation
```bash
pip install supers
```### Development installation
```bash
pip install -e .[tests,build]
```## Example
Say you have a class inheriting from multiple parent classes, and you would like to call a function for each parent. With `supers` this becomes as easy as:```python
from supers import supersclass Parent1:
def __init__(self, m:float):
self.m1 = m * 1def mult(self, value):
return value * self.m1class Parent2:
def __init__(self, m:float):
self.m2 = m * 2def mult(self, value):
return value * self.m2class Child(Parent1, Parent2):
def __init__(self, m):
supers(self).__init__(m)def allmult(self, val):
return supers(self).mult(val)c = Child(m=10)
# Parent attributes were updated
assert c.m1 == 10
assert c.m2 == 20# Parent1.mult is called as expected
assert c.mult(10) == 100# Each parent is called and results are returned in a list
assert c.allmult(10) == supers(c).mult(10) == [100, 200]```