Skip to content

Instantly share code, notes, and snippets.

@lbillingham
Last active June 9, 2017 20:07
Show Gist options
  • Save lbillingham/a17ccb86a3a8ac41724bbd97b68b216e to your computer and use it in GitHub Desktop.
Save lbillingham/a17ccb86a3a8ac41724bbd97b68b216e to your computer and use it in GitHub Desktop.
Stupid python chaining
class ChainableMeta(type):
"""this is in no way evil"""
def __getattr__(cls, _):
return Chainable()
class Chainable(object, metaclass=ChainableMeta):
"""
Can we do loads of chained attribute lookups
that always get to the same place?
"""
def __getattribute__(self, _):
return type(self)()
def __call__(self, *args, **kwargs):
return 'spam'
def test_instance():
"""can we call it?"""
assert Chainable()() =='spam'
def test_5th_level():
"""Can we nest it"""
really = Chainable.one.two.three.four.five()
assert really == 'spam'
def test_guess_what_im_using_this_for():
"""Is it production ready?"""
assert Chainable.objects.all() == 'spam'
assert Chainable.objects.filter(a__thing='What') == 'spam'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment