2015年7月1日星期三

【PYTHON】Become Pythonic

I have been using PYTHON for more than two year. But when I checked the programs in my first few years. I feel it is so hard to read. So I was thinking if there is a standard style or a method to make beautiful python program. Just like we write scientific papers. Fortunately, there are many discussions about "Be pythonic". Many useful links are included bellow.

(1)  PPT for 

Be pythonic @StudyArea 台南 2012 10月份

byHsinYi Chen




Some tips to use python

(1) While using interactive model in command line, you can use "_" instead of copying the number calculated in the last command line.
(2) exchange the values in a and b, simply type: b,a = a,b 
(3) String formation
     str.format = format(...)
     "{0} {1} {2}".format(1, 2, 3)
     "{a} {b} {c}".format(a=1, b=2, c=3)
     "%" is not recommend
(4) Useful operators:  lambda, filter, reduce, map
f = lambda x, y : x + y
r = map(func, seq)
result = filter(lambda x: x % 2, fib)
reduce(lambda x,y: x+y, [47,11,42,13])
(5) Learn to use Decorator.
       @decorator
       def function(a, b):
             return(a)
(6) get the path of ***
       the path of a module : os.path.realpath(module.__file__)
       the path of the current script : os.path.realpath(__file__)
(7)  exec、 eval、 execfile and compile  (a link)
      exec 'print "Hello World"'

To be continue








没有评论:

发表评论