【2017.04.20】
List Comprehension
Notes of Python Tutorial:
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Python 对象
is(), id(), isinstance()
type, class的统一;
is None 的真实含义
Slicing
两个索引参数构成先闭后开区间:
a = '12345'
>>> a[0:2]
'12'
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>> a[:2]
'12'
>>> a[1:]
'2345'
>>> a[:]
'12345'
slicing还支持第三个参数 'step' 表示步进:
>>> a[::1]
'12345'
>>> a[::2]
'135'
>>> a[::3]
'14'
>>> a[::4]
'15'
>>> a[::-1] 用这个方法可以实现list反转
'54321'
>>> a[::-2]
'531'
【2017.05.02】
Division
Python 3.x 之后
- real division: /
- floor division: //
TODO 字符编码/Unicode的知识一直没有搞清楚
三目运算
X if C else Y
Parameter Group
func(*tuple_grp_nonkw_args, **dict_grp_kw_args)
异常体系
- BaseException:
- Exception
- KeyboardException
- SystemExit
【2017.05.03】
装饰器(Decorator)
本质上decorator就是一个函数;与被装饰函数组成复合函数;
Python Language Refference & PEP
一些更深入的、与语言设计相关的内容
reduce
reduce(func, [1, 2, 3]) === reduce(func(1, 2), 3)
Generator
yield 与 coroutine的关系??
import
examples | suggest |
---|---|
import sys | good |
import sys, os, json | bad |
from module import name1[,name2] | OK |
from module import name1 | good |
from Tkinter import * | bad |
from cgi import FieldStorage as form | good |
Mutiple Inheritance
【20170517】
需要找另一个本书看下,
core p p 这本书整体讲的太啰嗦。
learning python 讲的可能好一点。
Classes and OOP
TODO 阅读 <Learning Python>
网友评论