Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more

Instantly share code, notes, and snippets.

@bmispelon bmispelon/evil.py
Created Nov 28, 2019

Embed
What would you like to do?
# https://mobile.twitter.com/dabeaz/status/1199367834691932160
"""
As a oneliner:
ᚐ = type('ᚐ',(object,),{'ᚐ':{},'__getattr__':lambda s,a:s.ᚐ[a],'__setattr__':lambda s,a,v:s.ᚐ.__setitem__(a,s.ᚐ[a]+v)if a in s.ᚐ else s.ᚐ.__setitem__(a,v)})()
for ᚐ.i, ᚐ.s in [(600, 'sa'), (60, 'ta'), (6, 'n')]:
pass
print(ᚐ.i, ᚐ.s)
"""
class AddOnAttributeAccess:
def __init__(self):
self.__dict__['_totals'] = {}
def __getattr__(self, attr):
return self._totals[attr]
def __setattr__(self, attr, value):
if attr in self._totals:
self._totals[attr] += value
else:
self._totals[attr] = value
if __name__ == '__main__':
adder = AddOnAttributeAccess()
for adder.i, adder.s in [(600, 'sa'), (60, 'ta'), (6, 'n')]:
pass
print(adder.i, adder.s)
@kmader

This comment has been minimized.

Copy link

kmader commented Nov 28, 2019

Did you manually minify or is there a python tool to do this?

@bmispelon

This comment has been minimized.

Copy link
Owner Author

bmispelon commented Nov 28, 2019

Did you manually minify or is there a python tool to do this?

This is all grain-fed, free-range, artisanally hand-crafted code. I don't know of any program that does this automatically (the if/else was quite tricky).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.