1、注释
注释以#号开头,不会被执行。>>> x = 10 # 定义一个变量x2、dir()函数查看对象内所有属性及方法。(1) 查看字符串类型。
>>> dir(str)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__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'](2) 查看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_WAIT', '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_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', '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', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']3、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='') -> stringReturn a nice string representation of the object.If the argument is a string, the return value is the same object.5、自定义文档自定义模块文件sample.py。
"""Module SampleThis 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文件,并进行操作。(1) dir方法
>>> import sample>>> dir(sample) # 模块sample['Sample', '__builtins__', '__doc__', '__file__', '__name__', '__package__']>>> dir(sample.Sample) # 模块内的类Sample['__doc__', '__module__', 'add', 'sub'](2) help方法
>>> help(sample) # 模块sampleHelp on module sample:NAME sampleFILE e:/sample.pyDESCRIPTION Module Sample This Module is for XXXXCLASSES 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)(3) __doc__方法
>>> print sample.__doc__ # 模块sample的doc文档Module SampleThis Module is for XXXX>>> print sample.Sample.__doc__ # 类Sample的doc文档class documentation class Sample
新闻热点
疑难解答