避免名称污染
尽量不要import *
-
Very bad
-
[...]
-
from modu import *
-
[...]
-
x = sqrt(4) # Is sqrt part of modu? A builtin? Defined above?
-
-
Better
-
from modu import sqrt
-
[...]
-
x = sqrt(4) # sqrt may be part of modu, if not redefined in between
-
-
Best
-
import modu
-
[...]
-
x = modu.sqrt(4) # sqrt is visibly part of modu
可变变量和不变变量
可变变量允许改变自身的值,典型的可变变量包括列表和字典。比如,所有的字典都有更改的方法,比如append和pop,可以原地更改。
然而,不变类型没有类似的方法来更改自己的内容。参考如下例子
,其中id()函数可以得到变量的地址,我们可以看到,字符串变量的地址发生了变化
-
>>> c = 'i'
-
>>> c
-
'i'
-
>>> id(c)
-
140467898860912
-
>>> c = c + ' am'
-
>>> c
-
'i am'
-
>>> id(c)
-
140467796846656
-
>>> dir
-
<built-in function dir>
-
>>> dir()
-
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'sys']
所以在处理字符串的时候,不要频繁的修改其值,否则会带来性能损失,参考如下例子。
-
Bad
-
# create a concatenated string from 0 to 19 (e.g. "012..1819")
-
nums = ""
-
for n in range(20):
-
nums += str(n) # slow and inefficient
-
print nums
-
-
Good
-
# create a concatenated string from 0 to 19 (e.g. "012..1819")
-
nums = []
-
for n in range(20):
-
nums.append(str(n))
-
print "".join(nums) # much more efficient
-
-
Best
-
# create a concatenated string from 0 to 19 (e.g. "012..1819")
-
nums = [str(n) for n in range(20)]
-
print "".join(nums)
字符串赋值
除了使用join() 和 + 以外,还可以利用 % 以及format函数来初始化字符串
-
foo = 'foo'
-
bar = 'bar'
-
-
foobar = '%s%s' % (foo, bar) # It is OK
-
foobar = '{0}{1}'.format(foo, bar) # It is better
-
foobar = '{foo}{bar}'.format(foo=foo, bar=bar) # It is best
取字典元素
不要使用has_key函数
-
Bad:
-
d = {'hello': 'world'}
-
if d.has_key('hello'):
-
print d['hello'] # prints 'world'
-
else:
-
print 'default_value'
-
-
Good:
-
d = {'hello': 'world'}
-
-
print d.get('hello', 'default_value') # prints 'world'
-
print d.get('thingy', 'default_value') # prints 'default_value'
-
-
# Or:
-
if 'hello' in d:
-
print d['hello']
列表操作
列表类型提供了一系列强大的操作方法,比如map和filter方法。
-
Bad:
-
# Filter elements greater than 4
-
a = [3, 4, 5]
-
b = []
-
for i in a:
-
if i > 4:
-
b.append(i)
-
-
Good:
-
b = [i for i in a if i > 4]
-
b = filter(lambda x: x > 4, a)
-
-
-
Bad:
-
# Add three to all list members.
-
a = [3, 4, 5]
-
count = 0
-
for i in a:
-
a[count] = i + 3
-
count = count + 1
-
-
Good:
-
a = [3, 4, 5]
-
a = [i + 3 for i in a]
-
# Or:
-
a = map(lambda i: i + 3, a)
使用enumerate函数来对列表进行计数,这种处理更易读,而且更高效。
-
for i, item in enumerate(a):
-
print i + ", " + item
-
# prints
-
# 0, 3
-
# 1, 4
-
# 2, 5
读文件
使用 with open 语法来读文件,这种方法会自动关闭文件,即使发生了异常。
-
Bad:
-
f = open('file.txt')
-
a = f.read()
-
print a
-
f.close()
-
-
Good:
-
with open('file.txt') as f:
-
for line in f:
-
print line
VIM 设置
-
set textwidth=79 " lines longer than 79 columns will be broken
-
set shiftwidth=4 " operation >> indents 4 columns; << unindents 4 columns
-
set tabstop=4 " an hard TAB displays as 4 columns
-
set expandtab " insert spaces when hitting TABs
-
set softtabstop=4 " insert/delete 4 spaces when hitting a TAB/BACKSPACE
-
set shiftround " round indent to multiple of 'shiftwidth'
-
set autoindent " align the new line indent with the previous line
阅读(2018) | 评论(0) | 转发(0) |