【收集】Python 微优化

1. 第二种方式可以节省寻找result的append属性的时间, 但会降低代码可读性和可维护性

# The way we're used to seeing it:
result.append("hello")

# But this works the same:
append_result = result.append
append_result("hello")

 

2. 将Python内置的方法在函数内重命名:寻找局部变量快于寻找全局变量和内置变量

# -*- coding:utf-8 -*-
# local to a function, global to a module, or built-in to Python
to_str = str

 

posted @ 2016-10-20 15:56  彭玉松  阅读(200)  评论(0编辑  收藏  举报