Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 
avatar of applicative_functor

applicative_functor

applicative_functor has asked 0 questions and find answers to 14 problems.

Stats

421
EtPoint
152
Vote count
0
questions
14
answers

About

>>> from collections import defaultdict
>>> import json

>>> def tree(): return defaultdict(tree)

>>> t = tree()
>>> t['a']['b'] = 'foo'
>>> t['a']['c'] = 'bar'
>>> json.dumps(t)
'{"a": {"c": "bar", "b": "foo"}}'

.

>>> from operator import methodcaller
>>> methodcaller('__call__', 2, 10)(pow)
1024

.

>>> getattr(__import__('sys'), 'stdout').write('hello\n')

.

>>> class A(object):
...     pass
... 
>>> a = A()
>>> b = A()
>>> def foo(self):
...     print "foo"
... 
>>> import types
>>> a.foo = types.MethodType(foo, a)
>>> a.foo()
foo
>>> b.foo()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'A' object has no attribute 'foo'