文章目录
Tips#11. 在Python中检查对象.
我们可以通过调用dir()方法来检查Python中的对象。这里有一个简单的例子。
test = [1, 3, 5, 7]
print( dir(test) )
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Tips#12. 简化if语句
为了验证多个值,我们可以采用以下方法。
if m in [1,3,5,7]:
替换
if m==1 or m==3 or m==5 or m==7:
或者,对于’ in ‘操作符,我们可以使用’{1,3,5,7}‘而不是’[1,3,5,7]’,因为’ set '可以通过O(1)访问每个元素。
Tips#13. 在运行时检测Python版本
有时,如果当前运行的Python引擎小于支持的版本,我们可能不想执行我们的程序。要实现这一点,您可以使用下面的代码片段。它还以可读格式打印当前使用的Python版本。
import sys
#Detect the Python version currently in use.
if not hasattr(sys, "hexversion") or sys.hexversion != 50660080:
print("Sorry, you aren't running on Python 3.5\n")
print("Please upgrade to 3.5.\n")
sys.exit(1)
#Print Python version in a readable format.
print("Current Python version: ", sys.version)
或者,您可以使用 sys.version_info >=(3,5)来替换。hexversion!= 50660080 在上述代码。
Python 2.7的输出
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
Sorry, you aren't running on Python 3.5
Please upgrade to 3.5.
Python 3.5的输出
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Current Python version: 3.5.2 (default, Aug 22 2016, 21:11:05)
[GCC 5.3.0]
Tips#14. 连接多个字符串
如果您想连接列表中所有字符串,请参见下面的示例。
>>> test = ['I', 'Like', 'Python', 'automation']
>>> print ''.join(test)
Tips#15. 四种方法反转字符串/列表。
倒转列表本身, 会改变列表
testList = [1, 3, 5]
testList.reverse()
print(testList)
#-> [5, 3, 1]
迭代反转
for element in reversed([1,3,5]): print(element)
#1-> 5
#2-> 3
#3-> 1
反转字符串
"Test Python"[::-1]
使用下标反转列表
[1, 3, 5][::-1]
Tips#16. 熟悉枚举
使用枚举数,当您处于循环中时,很容易找到索引。
testlist = [10, 20, 30]
for i, value in enumerate(testlist):
print(i, ': ', value)
#1-> 0 : 10
#2-> 1 : 20
#3-> 2 : 30
Tips#17. Python中枚举的使用
我们可以使用以下方法创建枚举定义.
class Shapes:
Circle, Square, Triangle, Quadrangle = range(4)
print(Shapes.Circle)
print(Shapes.Square)
print(Shapes.Triangle)
print(Shapes.Quadrangle)
#1-> 0
#2-> 1
#3-> 2
#4-> 3
Tips#18.从函数返回多个值
支持这个特性的编程语言并不多。然而,Python中的函数返回多个值。请参考下面的示例。
# function returning multiple values.
def x():
return 1, 2, 3, 4
# Calling the above function.
a, b, c, d = x()
print(a, b, c, d)
#-> 1 2 3 4
Tips#19. 使用 * 运算符(splat operator)来 unpack 函数参数
* 运算符(splat operator)提供了一个艺术化的方法来 unpack 参数列表,为清楚起见请参 见下面的例子:
def test(x, y, z):
print(x, y, z)
testDict = {'x': 1, 'y': 2, 'z': 3}
testList = [10, 20, 30]
test(*testDict)
test(**testDict)
test(*testList)
#1-> x y z
#2-> 1 2 3
#3-> 10 20 30
Tips#20. 使用字典来存储选择操作
可以使字典存储表达式。
stdcalc = {
'sum': lambda x, y: x + y,
'subtract': lambda x, y: x - y
}
print(stdcalc['sum'](9,3))
print(stdcalc['subtract'](9,3))
#1-> 12
#2-> 6
809

被折叠的 条评论
为什么被折叠?



