首先申明下,本文为笔者学习《Python学习手册》的笔记,并加入笔者自己的理解和归纳总结。
1. 注释
注释以#
号开头,不会被执行。
>>> x = 10 # 定义一个变量x
2. dir()函数
dir()
函数查看对象内所有属性及方法
查看字符串类型。
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__get
slice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mo
d__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook
__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center',
'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index
', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper',
'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', '
rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', '
strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
查看os
模块。
>>> import os
>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM',
'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT',
'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAI
T', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', '
X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list'
, '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_s
tatvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closera
nge', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error'
, 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe'
, 'extsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid',
'isatty', 'kill', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir',
'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3'
, 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdi
r', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_
float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tem
pnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', '
utime', 'waitpid', 'walk', 'write']
3. help()方法
help()
方法查看函数或模块用途的详细说明。
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
4. __doc__属性
>>> print str.__doc__
str(object='') -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
5、自定义文档
自定义模块文件sample.py
。
"""Module Sample
This Module is for XXXX
"""
class Sample:
"""class documentation
class Sample
"""
def add(self, a, b):
"""function add
return (a + b)
"""
return a + b
def sub(self, a, b):
"""function sub
return (a - b)
"""
return a - b
导入sample
文件,并进行操作。
dir()
方法
>>> import sample
>>> dir(sample) # 模块sample
['Sample', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> dir(sample.Sample) # 模块内的类Sample
['__doc__', '__module__', 'add', 'sub']
help()
方法
>>> help(sample) # 模块sample
Help on module sample:
NAME
sample
FILE
e:\sample.py
DESCRIPTION
Module Sample
This Module is for XXXX
CLASSES
Sample
class Sample
| class documentation
| class Sample
|
| Methods defined here:
|
| add(self, a, b)
| function add
| return (a + b)
|
| sub(self, a, b)
| function sub
| return (a - b)
__doc__
方法
>>> print sample.__doc__ # 模块sample的doc文档
Module Sample
This Module is for XXXX
>>> print sample.Sample.__doc__ # 类Sample的doc文档
class documentation
class Sample
相关文章
Python 数字类型(一)
Python 布尔型(二)
Python 字符串(三)
Python 列表(四)
Python 字典(五)
Python 元组(六)
Python 集合(七)
Python 变量和作用域(八)
Python 语句(九)
Python 函数(十)
Python 类(十一)
Python 模块(十二)
Python 文档(十三)
Python 文件(十四)
Python 异常(十五)
Python 运算符重载(十六)