Built-in objects preview
| Object type | Example literals/creation |
|---|---|
| Numbers | 1234, 3.1415, 3+4j, Decimal, Fraction |
| Strings | 'spam', "guido's", b'a\xolc' |
| Lists | [1,[2,'three'],4] |
| Dictionaries | {'foo':'spam','taste':'yum'}, dict(hours=10) |
| Tuples | (1,'spam',4,'U'), tuple('spam') |
| Files | myfile=open('eggs','r'), _myfile=open(r'C:\ham.bin','wb') |
| Sets | set('abc'), {'a','b','c'} |
| Other core types | Booleans, types, None |
| Program unit types | Functions, modules, classes |
| Implementation-related types | Complied code, stack tracebacks |
列表是一个序列,其元素是有序的,字典更像是映射(key -> value),元组根本上就是一个不可以改变的列表,集合是唯一的可变的对象的无序集合。
寻求帮助
使用 dir 函数可以查看对象的所有属性(方法也是对象的属性),使用 help 函数可以查看具体的方法:
1 | >>> S = '123' |
Python 编码规范
- Google Python Style Guide
- Style guide for Python code contributed to Melange
- Python 编码风格指南中译版(Google SOC)
- Google 开源项目风格指南 PYTHON风格规范
使用 pylint 进行代码检查
在 Windows 下安装 pylint :
1 | pip install pylint |
编写批处理测试脚本 pylint.bat ,方便测试:
1 | @echo off |
pylint.bat 使用:
1 | C:\Users\Leo>C:\Users\Leo\Desktop\pylint.bat F:\eclipse\workspaces\20140719\OOMMonitor\OOMMonitor.py |
输出结果在桌面的 pylint_log.txt 文件中,内容大致如下:
1 | ************* Module OOMMonitor |
输出内容中的 R,E,C 等代表的是 MESSAGE_TYPE ,其含义如下:
(C) 惯例。违反了编码风格标准
(R) 重构。写得非常糟糕的代码。
(W) 警告。某些 Python 特定的问题。
(E) 错误。很可能是代码中的错误。
(F) 致命错误。阻止 Pylint 进一步运行的错误。
Python 打包工具
Python metaclass
原地址:What is a metaclass in Python?
Python’s abc module
详细讲解地址:Abstract Base Classes in Python
推荐文章
Python技术文章收集:PyZh
