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__', '__re(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
新聞熱點
疑難解答