Sunday, June 8, 2014

star(*) and double star(**) in python

cc from http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters.
Mark here:
The *args and **kwargs is a common idiom to allow arbitrary number of arguments to functions.

The *args will give you all function parameters a list:
e.g.
    def foo(*args):
          for a in args:
               print a

The **kwargs will give you all keyword arguments.

  

Friday, May 23, 2014

How to improve python performance

recommended:
http://blog.monitis.com/2012/02/13/python-performance-tips-part-1/
http://blog.monitis.com/2012/03/21/python-performance-tips-part-2/
Summary:
(1) Use join() to glue a large number of strings.
(2) User python multiple assignment to swap variables.  (x, y = y, x).
(3) Use local variable if possible.
(4) Use in if possible.
(5) Speed up by lazy importing. (import modules when you really need them)
(6) Use "while 1" not "while True".
(7) Use list comprehension, also dict. (list = [i**3 for i in range(10)])
(8) Use xrange() for a very long sequence.
(9) Use python generator to get value on demand.
(10) Learn itertools module.
(11) Learn bisect module for keeping a list in sorted order.
(12) Understand that a Python list, is a actually an array.
(13) Use dict and set to test membership. (they are hash, list is array.)

中文译版:
http://www.oschina.net/question/1579_45822