关于dir()

时间:2014-01-12 23:04:21

标签: python function module dir

最初我试图找到dir(re),但意识到我有引号。

为清晰起见编辑:

"re"不是字符串吗?那么dir("re") == dir(string)?输出不一样。这基本上就是我想知道的。

编辑评论:

我可能会误解但是dir没有返回模块中所有函数的列表?当我在dir上拨打"re"时,它会给我一个功能列表。没有错误返回。

edit2:是dir。在红宝石和蟒蛇项目之间进行切换,出于某种原因,我对大脑进行了定制。对不起xD

1 个答案:

答案 0 :(得分:2)

我认为您需要的是澄清dir的作用。

dir是一个Python内置函数,根据是否提供参数,它可以执行以下两种操作之一:

  1. 如果没有参数,它将返回当前范围中的名称列表。这些名称表示为字符串。

  2. 使用参数,它返回属于该参数的属性和方法的列表。再一次,它将是一个字符串列表。

  3. 以下是第一个动作的演示:

    >>> num = 1 # Just to demonstrate
    >>> dir()
    ['__builtins__', '__doc__', '__name__', '__package__', 'num']
    >>>
    

    以下是第二个示例:

    >>> 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']
    >>>
    

    此外,如果其参数是Python模块,那么dir将列出该模块中包含的名称:

    >>> import sys
    >>> dir(sys)
    ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit',
    'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info',
    'warnoptions', 'winver']
    >>>
    

    现在,您在问题中说dir("re")的输出不等于dir(string)。关于这一点,我想说几点:

    1. 如果string是字符串文字,就像dir("string")一样,那么工作:

      >>> dir("re") == dir("string")
      True
      >>> 
      
    2. 如果"re"实际上是re模块,那么您所看到的行为是预期的,因为模块是一个字符串。

      < / LI>
    3. 如果您希望dir("re")列出re模块中包含的名称,那么您就错了。您不能使用字符串引用模块。相反,您必须先显式导入它:

      >>> dir("re")  # The output is the same as doing 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']
      >>>
      >>> import re
      >>> dir(re) # The output is for the re module, not str
      ['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall',  'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
      >>>