Last active
July 6, 2016 13:33
-
-
Save gyu-don/acb3ef83e4abd9635c599361ae021b47 to your computer and use it in GitHub Desktop.
tips for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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