??? ??
??? ??
1. ???
??? ?? UTF-8 ???? ?????.
??? ??? ??? #-*-?? ?? ??? ???? ???: UTF-8-*-Idential
2, ?? ??
2.1, ??
????? 4?? ? ???? ???? ??
2.2 , width
?? ?? 80?? ?? ??? ???? ???(??? ?? 80?? ?? ?? ? ??? ?? ??? 120? ?? ??? ???)
??:
??? ? ? ??? ???- by-side diffs
-
?? ???? ??? ?? ?? ?????
?? ?? ??? ??? ? ????
2.3, ?? ??
??? ??? ??? ????? ???? ?? ??? ?????? ????? ???? ???? ?????
? ???? ???. ???? ???? "..."
?? ??, ?? ???; ???? ?? ??? ???????. u"Hello World"
?? ?? '...'? ?????? ?????. ?? ?? dict
???? ?? ?? ???? r"..? ?????. ."
?? ???(docstring)? ???? ? ?? ?????. """.... .."""
2.4. ? ?
?? ?? ??? ??? ? ?? ? ?
??? ?? ?? ??? ??? ? ?? ????.
class A: def __init__(self): pass def hello(self): pass def main(): pass
?? ?? ? ?? ???? ?? ??? ?? ??? ??? ? ????.
? ?? ???? ????? ??? ??? ??? ? ????. ??
2.5, ???
??? UTF-8 ???? ?????
-
?? ??? #-*-conding:utf-8-*-??? ?????
3. import ?
import ?? ??? ?? ???? ???
# 正確的寫法 import os import sys # 不推薦的寫法 import sys,os # 正確的寫法 from subprocess import Popen, PIPE import語句應(yīng)該使用 absolute import # 正確的寫法 from foo.bar import Bar # 不推薦的寫法 from ..bar import Bar
import ?? ?? ?? ? ?? ??? ?, ?? ?? ?? ????? ???.
import ?? ? ??? ???? ? ?? ???? ???? ????? ???.import os import sys import msgpack import zmq import foo
from myclass import MyClass
import bar import foo.bar bar.Bar() foo.bar.Bar()
4, ??
??????? ??? ? ????. ?? ??? [=,-,+= ,==,>,in,is not, and]:
- ? ??? ?? ???
# 正確的寫法 i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) # 不推薦的寫法 i=i+1 submitted +=1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b)
- ?? ??? ??? ???.
# 正確的寫法 def complex(real, imag): pass # 不推薦的寫法 def complex(real,imag): pass
?? ???? ???? ???? ?? ??? ??? ???? ???
# 正確的寫法 def complex(real, imag=0.0): pass # 不推薦的寫法 def complex(real, imag = 0.0): pass
?? ?? ?, ??? ?? ?? ??? ???? ???
# 正確的寫法 spam(ham[1], {eggs: 2}) # 不推薦的寫法 spam( ham[1], { eggs : 2 } )
?? ??? ?? ?? ?? ?? ??? ????
# 正確的寫法 dict['key'] = list[index] # 不推薦的寫法 dict ['key'] = list [index]
???? ???? ? ?? ??? ???? ????
# 正確的寫法 x = 1 y = 2 long_variable = 3 # 不推薦的寫法 x = 1 y = 2 long_variable = 3
5 ? ??
Python? ? ??? ?????. ?? ??. ?? ??? ? ?????.
1. ? ?? ?? ?? ?? ???? ???????.
foo = long_function_name(var_one, var_two, var_three, var_four)
2. ? ?? ?? ?? 4?? ???????. ?? ?? ??? ??? ??? ?????.
def long_function_name( var_one, var_two, var_three, var_four): print(var_one)
???? ????? ?????. ?? ?? ?? + . ?? ? ?? ???? ???. ? ???? ? ??? ???? ? ??? ?? ????.
session.query(MyTable).\ filter_by(id=1).\ one() print 'Hello, '\ '%s %s!' %\ ('Harry', 'Potter')
?? ?? ?????. ?, ?? ?? ? ?? ?????.
# 正確的寫法 do_first() do_second() do_third() # 不推薦的寫法 do_first();do_second();do_third();
if/for/while? ?????? ???:
# 正確的寫法 if foo == 'blah': do_blah_thing() # 不推薦的寫法 if foo == 'blah': do_blash_thing()
6 , docstring
docstring ??? ?? ???? ? ?? ??:
1 ?? ?? ??, ??, ??? ? ???? docstring?? ????? ???. . Private ???? ??? ??? ?? ???? def ?? ?? ??? ???? ???.
2. ? ????? ? ?? ?? ??? ???? ????? ? """? ?? ??? ???? ???.