Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.7:dir()的新行为_Python_Python 3.7 - Fatal编程技术网

Python3.7:dir()的新行为

Python3.7:dir()的新行为,python,python-3.7,Python,Python 3.7,我刚刚安装了Python3.7,在inspect模块中遇到了dir()错误。请考虑这个最小的例子。 1) 创建一个有一行的模块testdir: __dir__ = 'bla' 2) 在python中: import testdir dir(testdir) 在Python 3.7中导致此错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' o

我刚刚安装了Python3.7,在
inspect
模块中遇到了dir()错误。请考虑这个最小的例子。

1) 创建一个有一行的模块
testdir

__dir__ = 'bla'
2) 在python中:

import testdir
dir(testdir)
在Python 3.7中导致此错误

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
我知道dir()希望
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。为什么行为不同?它是一个bug还是一个特性


谢谢

Python3.7现在也允许。以前它只是被忽略了。条件部分是,如果定义了
\uuuu dir\uuuu
,则它必须是可调用的,但不需要定义它。

Python3.7现在也允许。以前它只是被忽略了。条件部分是,如果定义了
\uuuu dir\uuu
,则它必须是可调用的,但不需要定义它。

相关的PEP()实际上包含规范:

\uuuu dir\uuuu
函数不应接受任何参数,并返回表示模块上可访问名称的字符串列表:

def __dir__() -> List[str]: ...
如果存在,此函数将覆盖模块上的标准dir()搜索

区别在于模块中的
\uuuu dir\uuu
在Python 3.7之前一直被忽略。鉴于必须根据Pythons规范使用双下划线名称,因此没有必要在其中包含不推荐(用于已更改的行为),因为
\uuuu dir\uuu
as字符串根本不符合规范。

相关的PEP()实际上包含规范:

\uuuu dir\uuuu
函数不应接受任何参数,并返回表示模块上可访问名称的字符串列表:

def __dir__() -> List[str]: ...
如果存在,此函数将覆盖模块上的标准dir()搜索


区别在于模块中的
\uuuu dir\uuu
在Python 3.7之前一直被忽略。鉴于必须根据Pythons规范使用双下划线名称,因此没有必要在其中包含弃用(用于已更改的行为),因为
\uuuu dir\uuuu
as字符串根本不符合规范。

deceze:感谢链接!这就解释了。PEP 562的实现应该检查
\uuuuu dir\uuuuuuu
是否可调用,但现在太迟了,我必须进行此“改进”。deceze:感谢链接!这就解释了。PEP 562的实现应该检查
\uuuuu dir\uuuuu
是否可调用,但现在已经太迟了,我必须进行此“改进”。尽管如此,实现可以检查
\uuuuuu dir\uuuuu
是否可调用