Skip to content

Instantly share code, notes, and snippets.

@gyu-don
Last active July 6, 2016 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyu-don/acb3ef83e4abd9635c599361ae021b47 to your computer and use it in GitHub Desktop.
Save gyu-don/acb3ef83e4abd9635c599361ae021b47 to your computer and use it in GitHub Desktop.
tips for python
class dotdict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
"""descriptions:
Like a JavaScript, get dictionary item via attribute.
d = dotdict()
d.a = 1
print(d.a) # ==> 1
print(d['a']) # ==> 1
d['b'] = 2
print(d.b) # ==> 2
"""
class pp_class:
__call__ = __rpow__ = __lt__ = __rfloordiv__ = lambda self, other: print(other) or other
pp = pp_class()
"""descriptions:
pp(123) prints "123" and return 123.
123 > pp, 123 ** pp, 123 // pp also same.
> is very low precedence operator, but higher than and, or, not, ...
** is very high precedence operator, but lower than x[i], x(args), x.attr, ...
// is also high precedence operator, but lower than **, unary +, -, ~
-123 ** pp prints 123
-123 // pp prints -123.
123 - 123 > pp prints 0,
123 - 123 ** pp, 123 - 123 // pp prints 123.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment