Jesse's Blog » 日志 » Notes_4_on <Learning Python>
Notes_4_on <Learning Python>
Jesse 发表于 2008-05-31 21:41:43
Part III .Statements and Syntax
1.
use simple for loops instead of while or range. P183
2.
Do not expect results from functions that change objects in-place. P183
E.g. for k in D.keys().sort(): #Wrong!
Part IV. Functions
3.
In Python, your code is not supposed to care about specific data types.
By and large, we code to object interface in Python, not data types. P194
4.
The enclosing module is a global scope;
The global scope spans a single file only. P198
5.
Enclosed function has access to names in enclosing functions.
def foo(y):
def bar():
y = 2
print y
foo(1)()
=============
python test.py >>> 2
6.
To erase thte '\n' end-line character in lines returned by readlines(): P233
[ line[:-1] for line in open('myfile').readlines()]
[ line[:-1] for line in open('myfile')]
## A qustion here: can the file, opened in list comprehensions, be manually closed? (manual closing is recommended, P116)
content = map( (lambda line: line[:-1], open('myfile')]
7.
To pick all values of a selected column from a table(possibly create by SQL)
[age for age (name, age, job) in list_of_tuple]
age_list = map( (lambda (name, age, job): age), list_of_tuple)
1.
use simple for loops instead of while or range. P183
2.
Do not expect results from functions that change objects in-place. P183
E.g. for k in D.keys().sort(): #Wrong!
Part IV. Functions
3.
In Python, your code is not supposed to care about specific data types.
By and large, we code to object interface in Python, not data types. P194
4.
The enclosing module is a global scope;
The global scope spans a single file only. P198
5.
Enclosed function has access to names in enclosing functions.
def foo(y):
def bar():
y = 2
print y
foo(1)()
=============
python test.py >>> 2
6.
To erase thte '\n' end-line character in lines returned by readlines(): P233
[ line[:-1] for line in open('myfile').readlines()]
[ line[:-1] for line in open('myfile')]
## A qustion here: can the file, opened in list comprehensions, be manually closed? (manual closing is recommended, P116)
content = map( (lambda line: line[:-1], open('myfile')]
7.
To pick all values of a selected column from a table(possibly create by SQL)
[age for age (name, age, job) in list_of_tuple]
age_list = map( (lambda (name, age, job): age), list_of_tuple)
收藏:
QQ书签
del.icio.us
订阅:
Google
抓虾
