Posts

Showing posts from June, 2015

Avoid initializing kwargs in signature as they might not work as expected when you need to pass kwargs from other methods

$ cat test_kwargs . py def m1 ( a , b = 2 ) : print a , b def m2 ( a , b = None ) : ​m1 ( a , b = b ) m1 ( 1 ) m2 ( 1 ) m1 ( 1 , 2 ) m2 ( 1 , 2 ) $ python test_kwargs . py 1 2 1 None 1 2 1 2 A better version of the above methods would be, ​ $ cat test_kwargs . py def m1 ( a , b = None ) : if b is None : b = 2 print a , b def m2 ( a , b = None ) : m1 ( a , b = b ) m1 ( 1 ) m2 ( 1 ) m1 ( 1 , 2 ) m2 ( 1 , 2 ) $ python test_kwargs . py 1 2 1 2 1 2 1 2​

Understanding multiple inheritance and method resolution order in Python

# cat multiple_inheritance.py import inspect class A ( object ): pass class B (A): pass class C (A): pass class D (B): pass class E (C): pass class F (D, E): pass print inspect.getmro(A) print inspect.getmro(B) print inspect.getmro(C) print inspect.getmro(D) print inspect.getmro(E) print inspect.getmro(F) # python multiple_inheritance.py (< class ' __main__ .A '>, <type ' object '>) (< class ' __main__ .B '>, <class ' __main__.A '>, <type ' object '>) (< class ' __main__ .C '>, <class ' __main__.A '>, <type ' object '>) (< class ' __main__ .D '>, <class ' __main__.B '>, <class ' __main__.A '>, <type ' object '>) (< class ' __main__ .E '>, <class ' __main__.C '>, <class ' __main__.A '>, <type ' object '>) (< class '