python文档

形式 # 角色

注释 文件中的文档

dir函数 对象中可用属性的列表

文档字符串__doc__ 附加在对象文件中的文档

标准手册 正式的语言和库的说明

网站 在线教程,例子

书籍 商业参考书籍

注释

代码编写的最基本的方式,文档字符串用于较大功能的文档

而# 用于较小功能的文档

dir函数

抓取对象内可用的所有属性列表的简单方式

import random

print(dir(random))

['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST',

'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence',

'_Set', 'all', 'builtins', 'cached', 'doc', 'file',

'loader', 'name', 'package', 'spec', '_acos', '_bisect',

'_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random',

'_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn',

'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss',

'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate',

'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle',

'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

print(dir('1') == dir(''))

True

__doc__文档字符串,这类注释是写成字符串,放在模块文件,函数以及语句的顶端

在可执行代码执行前,会自动封装这个字符串,也就是文档字符串,使他成为__doc__

属性

内置文档字符串可以用__doc_来查看

import sys

print(sys.doc)

...

print(sys.getrefcount.doc)

getrefcount(object) -> integer

Return the reference count of object. The count returned is generally

one higher than you might expect, because it includes the (temporary)

reference as an argument to getrefcount().

print(int.doc)

...

help函数

启动pydoc来查看文档,如help函数和PyDocGUI、HTML接口

print(help(int))

...

常见编写代码陷阱

别忘了复合语句末尾输入':'

要确定顶层程序代码从第1行开始

空白行在交互模式下是告诉交互模式命令行完成复合语句

缩进要一致,尽量使用统一缩进,统一制表符或者四个空格

不要在python中写c代码。因为这样不pythonic

使用简单的for循环或者解析,而不是while和range

要注意赋值语句中的可变对象。a = b = [],a += [1, 2]都会在原处修改

# 会影响其他变量

不要期待在原处修改对象的函数返回结果,[1,2,3],append(4)他们只会返回None

要使用括号调用函数,不然只会返回他们的函数命名空间

不要在导入和重载中使用扩展名或者路径 import mod 而不是import mod.py

posted on 2019-07-19 08:48  幻嘤剑舞  阅读(95)  评论(0编辑  收藏  举报

导航