blabla

Decorator

http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python

Closure

http://stackoverflow.com/questions/4020419/closures-in-python

Yield

http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python

Use travis ci in github for your python projects

Higher order functions

filter, map, reduce, sum, any, all.

integers = range(10)
even = filter(lambda x: x % 2 ==0, integers)
even = [x for x in integers if x % 2 == 0]
map(lambda x: x * x, integers)
reduce(lambda x, y: x * y, integers)
sum(integers)
any([False, True, False])
all([False, True, False])

super() in python

String.format

一般的:

"{}, {}".format("Hello", "world")

可以指定顺序,或者重复某个:

"{2}{0}{1}".format('1', '2', '3')
"{1}{0}{1}".format('1', '2', '3')

使用关键字:

"{first}{second}".format(first='Hello', second='world')

使用tuple, dict

list_number = ('1', '2', '3')
"{}{}{}".format(*list_number)
dictory_number = {'first': '1', 'second': '2'}
"{first}{secong}".format(**dictory_number)

格式化:

[[fill]align][sign][#][0][minimumwidth][.precision][type]

align flags:
    '<' - left-aligned, fill with space.
    '>' - right-aligned
    '=' - valid only for numeric types, force the padding to be placed
        - after the sign but before the digits.
    '^' - centered

sign(only valid for numeric types):
    '+' - a sign should be used for both positive as well as negative numbers.
    '-' - a sign only for negative numbers.
    ' ' - a leading space should be used on positive numbers.

'#': integers use the 'alternate form' which means taht binary, octal and
     hexadecimal output will be prefixed with 0b, 0o, 0x.

Inside the {}, we can't put executable python code except: (1):retrive attibute with '.' notation.(2):retrive a single item with '[]' notation.

Reference:

standard library

re

groups group

unicode

  sys.stdout = codes.getwriter('utf-8')(sys.stdout)
  text = u''
  pattern = ur'\w+'
  re.search(pattern, text)

\num or (?P=name): 自引用表达式

re.split()

pydoc

$ pydoc platform  # Get help information about platform module
Or in the interpreter
>> help('platform')
>> help('platform.system')
Or browse in the browser
$ pydoc -p PORT

timeit

Class: Timer(stmt='pass', setup='pass', timer=)

functions:

from terminal

$ python -m timeit "for i in range(1000)" "    d[str(i)=i"
$ pthon -m timeit "import hello; hello.say_hi()"

the difference between __str__ and __repr__

object.__repr__(self)

Called by the repr() built-in function and by string conversions (reverse
quotes) to compute the official string representation of an object. If
at all possible, this should look like a valid Python expression that
could be used to recreate an object with the same value (given an
appropriate environment). If this is not possible, a string of the form
<...some useful description...> should be returned. The return value must
be a string object. If a class defines __repr__() but not __str__(),
then __repr__() is also used when an informal string representation of
instances of that class is required.

This is typically used for debugging, so it is important that the
representation is information-rich and unambiguous.

object.__str__(self)

Called by the str() built-in function and by the print statement to
compute the informal string representation of an object. This differs
from __repr__() in that it does not have to be a valid Python expression:
a more convenient or concise representation may be used instead. The
return value must be a string object.

Exception

relavited blog posts:

StackOverflow relavited questions: