zoukankan      html  css  js  c++  java
  • python dir 自省方法

    内置的dir()函数是用来找出一个模块内置的方法。它返回一个字符串列表

    >>> import fibo, sys
    >>> dir(fibo)
    ['__name__''fib''fib2']
    >>> dir(sys)
    ['__displayhook__''__doc__''__excepthook__''__name__''__stderr__',
    '__stdin__''__stdout__''_getframe''api_version''argv',
    'builtin_module_names''byteorder''callstats''copyright',
    'displayhook''exc_clear''exc_info''exc_type''excepthook',
    'exec_prefix''executable''exit''getdefaultencoding''getdlopenflags',
    'getrecursionlimit''getrefcount''hexversion''maxint''maxunicode',
    'meta_path''modules''path''path_hooks''path_importer_cache',
    'platform''prefix''ps1''ps2''setcheckinterval''setdlopenflags',
    'setprofile''setrecursionlimit''settrace''stderr''stdin''stdout',
    'version''version_info''warnoptions']

     如果dir()方法没有参数,则列出你目前已经定义的变量名称:

    >>> a = [12345]
    >>> import fibo
    >>> fib = fibo.fib
    >>> dir()
    ['__builtins__''__doc__''__file__''__name__''a''fib''fibo''sys']

    如果你想查看内置函数和系统关键字,可以用标准模块__ builtin__: 请注意,它列出了所有类型的名称的变量,模块,系统关键字等。

    >>> import __builtin__
    >>> dir(__builtin__)
    ['ArithmeticError''AssertionError''AttributeError''DeprecationWarning',
    'EOFError''Ellipsis''EnvironmentError''Exception''False',
    'FloatingPointError''FutureWarning''IOError''ImportError',
    'IndentationError''IndexError''KeyError''KeyboardInterrupt',
    'LookupError''MemoryError''NameError''None''NotImplemented',
    'NotImplementedError''OSError''OverflowError',
    'PendingDeprecationWarning''ReferenceError''RuntimeError',
    'RuntimeWarning''StandardError''StopIteration''SyntaxError',
    'SyntaxWarning''SystemError''SystemExit''TabError''True',
    'TypeError''UnboundLocalError''UnicodeDecodeError',
    'UnicodeEncodeError''UnicodeError''UnicodeTranslateError',
    'UserWarning''ValueError''Warning''WindowsError',
    'ZeroDivisionError''_''__debug__''__doc__''__import__',
    '__name__''abs''apply''basestring''bool''buffer',
    'callable''chr''classmethod''cmp''coerce''compile',
    'complex''copyright''credits''delattr''dict''dir''divmod',
    'enumerate''eval''execfile''exit''file''filter''float',
    'frozenset''getattr''globals''hasattr''hash''help''hex',
    'id''input''int''intern''isinstance''issubclass''iter',
    'len''license''list''locals''long''map''max''min',
    'object''oct''open''ord''pow''property''quit''range',
    'raw_input''reduce''reload''repr''reversed''round''set',
    'setattr''slice''sorted''staticmethod''str''sum''super',
    'tuple''type''unichr''unicode''vars''xrange''zip']
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/wanpython/p/3073514.html
Copyright © 2011-2022 走看看