代码结构
1 . 什么时候用装饰器:
【https: // learnku. com/ docs/ python- guide/ 2018 / structure/ 3260 】
def func_01 ( ) :
def decorator ( func) :
return func
decorator( func_01)
@decorator
def func_02 ( ) :
2 . 创建一个连续的字符串:
【https: // learnku. com/ docs/ python- guide/ 2018 / structure/ 3260 】
nums = ""
for n in range ( 20 ) :
nums += str ( n)
print ( nums)
nums = [ ]
for n in range ( 20 ) :
nums. append( str ( n) )
print ( "" . join( nums) )
nums = [ str ( n) for n in range ( 20 ) ]
print "" . join( nums)
nums = map ( str , range ( 20 ) )
print "" . join( nums)
2.编码风格
1 . 查看列表中是否存在某个值:
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
s = set ( [ 's' , 'p' , 'a' , 'm' ] )
l = [ 's' , 'p' , 'a' , 'm' ]
if 's' in s:
print ( "True" )
if 's' in l:
print ( "True" )
原因:
1 . 为了判断一个项是否在列表中,Python 将会查看每个项直到它找到匹配的项。这是耗时的任务,尤其是对长列表而言。
2 . 在集合中,项的哈希值将会告诉 Python 在集合的哪里去查找匹配的项。
3 . 在字典中查询也是同样的原理。(查看第5 个)
2. Python之禅:
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
>> > import this
优美胜于丑陋 (Python 以编写优美的代码为目标)
明了胜于晦涩 (优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂 (优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱 (如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套 (优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑 (优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要 (优美的代码是具备高可读性的)
即便假借特例的实用性之名,也不可违背这些规则 (这些规则至高无上)
不要包容所有错误,除非您确定需要这样做 (精准地捕获异常,不写 `except : pass ` 风格的代码)
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案 (如果不确定,就用穷举法)
虽然这并不容易,因为您不是 Python 之父 (这里的 Dutch 是指 Guido )
做也许好过不做,但不假思索就动手还不如不做 (动手之前要细思量)
如果您无法向人描述您的方案,那肯定不是一个好方案;反之亦然 (方案测评标准)
命名空间是一种绝妙的理念,我们应当多加利用 (倡导与号召)
3. PEP8代码风格
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
pip install pycodestyle
>> > pycodestyle optparse. py
pip install autopep8
>> > autopep8 - - in - place tmp. py
4 . 检查变量是否等于常量
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
if attr == True :
pass
if attr == None :
pass
if attr:
pass
if not attr:
pass
if attr is None :
pass
5 . 访问字典元素
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
d = { 'hello' : 'world' }
if d. has_key( 'hello' ) :
print ( d[ 'hello' ] )
else :
print ( 'default_value' )
if "hello" in set ( d) :
print ( d[ 'hello' ] )
else :
print ( 'default_value' )
print ( d. get( 'hello' , 'default_value' ) )
6 . 操作列表
【https: // learnku. com/ docs/ python- guide/ 2018 / writing- style/ 3261 】
a = [ 3 , 4 , 5 ]
b = [ ]
for i in a:
if i > 4 :
b. append( i)
print ( b)
b = [ i for i in a if i > 4 ]
print ( b)
b = filter ( lambda x: x > 4 , a)
print ( list ( b) )
代码测试
import unittest
import doctest
def fun ( x) :
"""
摘要:返回x加1的值
扩展功能描述:
pass
:param x:
pass
:return:
pass
>>> fun(3)
4
>>> fun(-1)
0
"""
return x + 1
def square ( x) :
"""
摘要:返回x的幂次方的值
扩展功能描述:
pass
:param x:
pass
:return:
pass
>>> square(2)
4
>>> square(-2)
4
"""
return x * x
def test_answer ( ) :
assert fun( 3 ) == 5
class TestStringMethods ( unittest. TestCase) :
def test_fun ( self) :
self. assertEqual( fun( 3 ) , 4 )
def test_upper ( self) :
self. assertEqual( 'foo' . upper( ) , 'FOO' )
def test_isupper ( self) :
self. assertTrue( 'FOO' . isupper( ) )
self. assertFalse( 'Foo' . isupper( ) )
def test_split ( self) :
s = 'hello world'
self. assertEqual( s. split( ) , [ 'hello' , 'world' ] )
with self. assertRaises( TypeError) :
s. split( 2 )
if __name__ == '__main__' :
unittest. main( )
doctest. testmod( )