1. 程式人生 > >python 程式設計中要注意的事情

python 程式設計中要注意的事情

記住python中那些非常簡單的事

a, b = b, a
a= [1,2,3,4,5]
>>>a[::2]
[1,3,5]
一個特殊的例子 x[::-1]用來反轉x

不要用可變物件作為預設引數值(Don’t use mutable as defaults)

def function(x, l=[]):      # 不要這麼幹
def function(x, l=None):    # 更好的一種方式
    if l is None:
       l = []

這是因為函式的預設值實在函式定義的時候例項化的,不是在函式呼叫的時候。
但是,預設值型別是可變變數的時候才會出現這種情況。
例:

def foo(numbers=[]):
    numbers.append(9)
    print numbers
>>> foo() # first time, like before
[9]
>>> foo() # second time
[9, 9]
>>> foo() # third time...
[9, 9, 9]
>>> foo() # WHAT IS THIS BLACK MAGIC?!
[9, 9, 9, 9]
#因為每次都是呼叫的預設值,所以預設值改變




>>> foo()
[9
] >>> foo(numbers=[1,2]) [1, 2, 9] >>> foo(numbers=[1,2,3]) [1, 2, 3, 9]#重寫預設值,所以不會出現上面的問題 def foo(count=0): count += 1 print count 10 >>> foo() 1 >>> foo() 1 >>> foo(2) 3 >>> foo(3) 4 >>> foo() 1 #這是因為整型是個不可變變數,所以預設值始終不會改變,不會出現上面的問題。
def print_now(now=time.time()): print now 2 3 4 5 6 >>> print_now() 1373121487.91 >>> print_now() 1373121487.91 >>> print_now() 1373121487.91 #它是在函式定義時計算的,所以無論呼叫多少次都不會變。

使用iteritems而不是items

d = {1: "1", 2: "2", 3: "3"}

for key, val in d.items()       # 呼叫items()後會構建一個完整的list物件

for key, val in d.iteritems()   # 只有在迭代時每請求一次才生成一個值

使用isinstance 而不是type

不要這樣做:

if type(s) == type(""): ...
if type(seq) == list or \
    type(seq) == tuple: ...

應該是這樣:

if isinstance(s, basestring): ...
if isinstance(seq, (list, tuple)): ...

學習各種集合(learn the various collections)
python有各種各樣的容器資料型別,在特定情況下選擇python內建的容器如:list和dict。通常更多像如下方式使用:

  freqs = {}
  for c in "abracadabra":
      try:
          freqs[c] += 1
      except:
          freqs[c] = 1

一種更好的方案如下:

freqs = {}
   for c in "abracadabra":
       freqs[c] = freqs.get(c, 0) + 1

一種更好的選擇 collection型別defautdict:

from collections import defaultdict
freqs = defaultdict(int)
    for c in "abracadabra":
        freqs[c] += 1

當建立類時Python的魔術方法:

  __eq__(self, other)      # 定義相等操作的行為, ==.
  __ne__(self, other)      # 定義不相等操作的行為, !=.
  __lt__(self, other)      #定義小於操作的行為, <.
  __gt__(self, other)      #定義不大於操作的行為, >.
  __le__(self, other)      #定義小於等於操作的行為, <=.
  __ge__(self, other)      #定義大於等於操作的行為, >=.

條件賦值重點內容
表示式請起來恰恰像:如果y等於1就把3賦值給x,否則把2賦值給x,當然同樣可以使用鏈式條件賦值如果你還有更復雜的條件的話。

(x = 3 if (y == 1) else 2 if (y == -1) else 1

然而到了某個特定的點,它就有點兒過分了。

記住,你可以在任何表示式中使用if-else例如:

c1 if y == 1 else func2)(arg1, arg2)

func1將被呼叫如果y等於1的話,反之func2被呼叫。兩種情況下,arg1和arg2兩個引數都將附帶在相應的函式中。

類似地,下面這個表示式同樣是正確的

x = (class1 if y == 1 else class2)(arg1, arg2)

class1和class2是兩個類

帶有索引位置的集合遍歷
遍歷集合時如果需要使用到集合的索引位置時,直接對集合迭代是沒有索引資訊的,普通的方式使用:

colors = ['red', 'green', 'blue', 'yellow']

for i in range(len(colors)):
    print (i, '--->', colors[i])

應該用:

for i, color in enumerate(colors):
    print (i, '--->', color)

字串連線
字串連線時,普通的方式可以用 + 操作:

names = ['raymond', 'rachel', 'matthew', 'roger',
         'betty', 'melissa', 'judith', 'charlie']

s = names[0]
for name in names[1:]:
    s += ', ' + name
print (s)

應該用:

print (', '.join(names))