It still works, I just don't plan to update it anymore. I also released into the public domain, so feel free to do whatever with it.
A metaclass for automatic method chaining. (Only for methods that return None
, obviously)
from nchainz import Chainz
class A(metaclass=Chainz):
def has_return_value(self):
return 4
def this_is_chainable(self):
print("hello")
# implicitly returns self
a = A()
assert a is a.this_is_chainable().this_is_chainable()
assert a.has_return_value() == 4
Method chaining describes the Syntax of not having to assign objects between methods which are changing the state.
For example, in JS one can just chain the array transformations like
[1,2,3,4,5,6].filter(x => x % 2 == 0).map(x => x * x).find(x => x > 30)
Pretty easy. You just return self
. Here is an example:
class MyNum:
def __init__(self, x):
self.x = x
def inc(self):
self.x += 1
return self
three = MyNum(3)
six = three.inc().inc().inc()
assert three.x+3 == six.x
pip install nchainz
Just use the Chainz
metaclass:
from nchainz import Chainz
class MyClass(metaclass=Chainz):
...
I had my 5 minutes, I am sorry.
It's such a great read, you should really read it. (Written for Python 2)