首页
学习
活动
专区
圈层
工具
发布

关于dir()
EN

Stack Overflow用户
提问于 2014-01-12 23:04:20
回答 1查看 102关注 0票数 0

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

为清晰而编辑:

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

编辑备注:

我可能误解了,但是dir没有返回模块中所有函数的列表吗?另外,当我在dir上调用"re"时,它给出了一个函数列表。不返回错误。

edit2:是的,dir。我一直在一个红宝石和python项目之间切换,出于某种原因,我对大脑进行了研究。对不起,xD

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-13 01:17:23

我认为您需要的是澄清dir所做的事情。

dir是一个内置函数,它执行两种操作之一,这取决于是否提供了参数:

  1. 如果没有参数,它将返回当前作用域中的名称列表。这些名称表示为字符串。
  2. 对于参数,它返回属于该参数的属性和方法的列表。再一次,它将是一个字符串列表。

以下是第一项行动的示范:

代码语言:javascript
复制
>>> num = 1 # Just to demonstrate
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'num']
>>>

下面是第二个例子:

代码语言:javascript
复制
>>> 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将列出包含在该模块中的名称:

代码语言:javascript
复制
>>> 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("string") True >>>
  2. 如果"re"实际上是re模块,那么您所看到的行为是预期的,因为该模块不是字符串。
  3. 如果您期望dir("re")列出re模块中包含的名称,那么您就错了。不能用字符串引用模块。相反,您必须先显式导入它:dir("re") #输出与执行dir(str)‘_add__’、'__class__‘、'__contains__’、‘__delattr_’、'__doc__‘、'__eq__’、_format_、‘__ge_’、‘_get属性__’、‘__geti_’、‘__getnewargs_’、‘__getnewargs_’、‘_getnewargs_’、‘_’、_、_‘、_getnewargs_’、_getnewargs_‘_gt_’,‘___rmul_‘_“‘islower”、“isspace”、“istitle”、“isupper”、“join”、“ljust”、“lower”、“lstrip”、“分区”、“替换”、“rfind”、“rindex”、“rstrip”、“partition”、“rsplit”、“split”、“splitlines”、“startswith”、“strip”、“swapcase”、“title”、“翻译”、“上方”,>>> >>> import re >>> dir( re ) #输出的是re模块,而不是str 'DEBUG','DOTALL','I','IGNORECASE','L','LOCALE','M','MULTILINE','S',‘扫描仪’,'T',‘模板’,'U','UNICODE',‘详细’,'X','_MAXCACHE',‘_all__’,'__all__',‘__builtins_’,‘_doc__’,‘__file_’,__name_‘,_version_’,'_alphanum','_cache','_cache_repl','_compile_repl','_expand','_pattern_type',‘_咸菜’,'_subx',‘编译’,'copy_reg',‘错误’,‘转义’,'findall',“‘finditer”、“match”、“purge”、“search”、“split”、“sre_compile”、“sre_parse”、“sub”、“subn”、“sys”、“>>>”
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21081467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档