Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2404890
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: Python/Ruby

2013-05-07 13:37:24

避免名称污染

尽量不要import *
  1. Very bad
  2. [...]
  3. from modu import *
  4. [...]
  5. x = sqrt(4) # Is sqrt part of modu? A builtin? Defined above?

  6. Better
  7. from modu import sqrt
  8. [...]
  9. x = sqrt(4) # sqrt may be part of modu, if not redefined in between

  10. Best
  11. import modu
  12. [...]
  13. x = modu.sqrt(4) # sqrt is visibly part of modu

可变变量和不变变量

可变变量允许改变自身的值,典型的可变变量包括列表和字典。比如,所有的字典都有更改的方法,比如append和pop,可以原地更改。
然而,不变类型没有类似的方法来更改自己的内容。参考如下例子,其中id()函数可以得到变量的地址,我们可以看到,字符串变量的地址发生了变化
  1. >>> c = 'i'
  2. >>> c
  3. 'i'
  4. >>> id(c)
  5. 140467898860912
  6. >>> c = c + ' am'
  7. >>> c
  8. 'i am'
  9. >>> id(c)
  10. 140467796846656
  11. >>> dir
  12. <built-in function dir>
  13. >>> dir()
  14. ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'sys']
所以在处理字符串的时候,不要频繁的修改其值,否则会带来性能损失,参考如下例子。

  1. Bad
  2. # create a concatenated string from 0 to 19 (e.g. "012..1819")
  3. nums = ""
  4. for n in range(20):
  5.   nums += str(n) # slow and inefficient
  6. print nums

  7. Good
  8. # create a concatenated string from 0 to 19 (e.g. "012..1819")
  9. nums = []
  10. for n in range(20):
  11.   nums.append(str(n))
  12. print "".join(nums) # much more efficient

  13. Best
  14. # create a concatenated string from 0 to 19 (e.g. "012..1819")
  15. nums = [str(n) for n in range(20)]
  16. print "".join(nums)

字符串赋值

除了使用join() 和 + 以外,还可以利用 % 以及format函数来初始化字符串

  1. foo = 'foo'
  2. bar = 'bar'

  3. foobar = '%s%s' % (foo, bar) # It is OK
  4. foobar = '{0}{1}'.format(foo, bar) # It is better
  5. foobar = '{foo}{bar}'.format(foo=foo, bar=bar) # It is best

取字典元素

不要使用has_key函数

  1. Bad:
  2. d = {'hello': 'world'}
  3. if d.has_key('hello'):
  4.     print d['hello'] # prints 'world'
  5. else:
  6.     print 'default_value'

  7. Good:
  8. d = {'hello': 'world'}

  9. print d.get('hello', 'default_value') # prints 'world'
  10. print d.get('thingy', 'default_value') # prints 'default_value'

  11. # Or:
  12. if 'hello' in d:
  13.     print d['hello']

列表操作

列表类型提供了一系列强大的操作方法,比如map和filter方法。

  1. Bad:
  2. # Filter elements greater than 4
  3. a = [3, 4, 5]
  4. b = []
  5. for i in a:
  6.     if i > 4:
  7.         b.append(i)

  8. Good:
  9. b = [i for i in a if i > 4]
  10. b = filter(lambda x: x > 4, a)


  11. Bad:
  12. # Add three to all list members.
  13. a = [3, 4, 5]
  14. count = 0
  15. for i in a:
  16.     a[count] = i + 3
  17.     count = count + 1

  18. Good:
  19. a = [3, 4, 5]
  20. a = [i + 3 for i in a]
  21. # Or:
  22. a = map(lambda i: i + 3, a)
使用enumerate函数来对列表进行计数,这种处理更易读,而且更高效。

  1. for i, item in enumerate(a):
  2.     print i + ", " + item
  3. # prints
  4. # 0, 3
  5. # 1, 4
  6. # 2, 5

读文件

使用 with open 语法来读文件,这种方法会自动关闭文件,即使发生了异常。

  1. Bad:
  2. f = open('file.txt')
  3. a = f.read()
  4. print a
  5. f.close()

  6. Good:
  7. with open('file.txt') as f:
  8.     for line in f:
  9.         print line

VIM 设置


  1. set textwidth=79 " lines longer than 79 columns will be broken
  2. set shiftwidth=4 " operation >> indents 4 columns; << unindents 4 columns
  3. set tabstop=4 " an hard TAB displays as 4 columns
  4. set expandtab " insert spaces when hitting TABs
  5. set softtabstop=4 " insert/delete 4 spaces when hitting a TAB/BACKSPACE
  6. set shiftround " round indent to multiple of 'shiftwidth'
  7. set autoindent " align the new line indent with the previous line

阅读(1986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~