总结 | 提高 Python 代码质量的 7 个习惯
功能性的代码每个人都可以写,但能写出优雅的程序却没那么容易,因此程序写的优雅与否则是区分顶级程序员与一般程序员的终极指标所在。
0x00 规范命名
a,b,c x,y,z a1,a2 4_s,4s...
def do_something():
def fun():
...
0x01 面向对象
0x02 使用 with
open()
函数来打开一个文件,最后等操作完成之后通过
close()
函数来关闭文件,但有时候写多了难免会觉得很麻烦,难道不可以在我操作完自动关闭文件么,可以的。使用 with 来操作文件无需考虑关闭问题,我们只需要关心核心的业务逻辑即可。
with open('tmp.txt', 'w') as f:
f.write('xxx')
...
0x03 使用 get
KeyError
。这时候建议通过
get()
函数来获取。
get()
函数来获取 value 时还可以设置默认值 default_value,当 key 不存在时则会返回 default_value。
0x04 提前返回
score = 100
if score >= 60: # 及格
if score >= 70: # 中等
if score >= 80: # 良好
if score >= 90: # 优秀
if score >= 100: # 满分
print("满分")
else:
print("优秀")
else:
print("良好")
else:
print("中等")
else:
print("及格")
else:
print("不及格")
print("程序结束")
def get_score_level(score):
if score >= 100: # 满分
print("满分")
return
if score >= 90: # 优秀
print("优秀")
return
if score >= 80: # 良好
print("良好")
return
if score >= 70: # 中等
print("中等")
return
if score >= 60: # 及格
print("及格")
return
print("不及格")
print("程序结束")
0x05 生成器
0x06 装饰器
def hi(name="yasoob"):
print("now you are inside the hi() function")
def greet():
return "now you are in the greet() function"
def welcome():
return "now you are in the welcome() function"
print(greet())
print(welcome())
print("now you are back in the hi() function")
hi()
# output
# now you are inside the hi() function
# now you are in the greet() function
# now you are in the welcome() function
# now you are back in the hi() function
hi()
函数内部定义了两个新的函数,无论何时调用
hi()
其内部的函数都将会被调用。
def hi(name="yasoob"):
def greet():
return "now you are in the greet() function"
def welcome():
return "now you are in the welcome() function"
if name == "yasoob":
return greet
else:
return welcome
a = hi()
print(a)
print(a())
# output
# <function hi.<locals>.greet at 0x7fe3e547a0e0>
# now you are in the greet() function
name = yasoob
因此
a = hi()
返回的是
greet
函数。a 也就指向了
hi()
函数内部的
greet()
函数。
def hi():
return "hi yasoob!"
def doSomethingBeforeHi(func):
print("I am doing some boring work before executing hi()")
print(func())
doSomethingBeforeHi(hi)
# output
# I am doing some boring work before executing hi()
# hi yasoob!
hi()
函数传递给了另外一个函数,并且他们还很愉快的执行了。
def a_new_decorator(a_func):
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
a_new_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
a_new_function_requiring_decoration()
# output
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
@
。
def a_new_decorator(a_func):
...
@a_new_decorator
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
a_function_requiring_decoration()
# output
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
总结
- EOF -
1、
2、
3、
觉得本文对你有帮助?请分享给更多人
推荐关注「Python开发者」,提升Python技能
点赞和在看就是最大的支持❤️