【发布时间】:2017-09-21 11:28:13
【问题描述】:
$ echo "Your code is bad and you should feel bad" > "<stdin>"
$ python
Python 3.6.0 (default, Dec 28 2016, 19:53:26)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + '2'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Your code is bad and you should feel bad
TypeError: unsupported operand type(s) for +: 'int' and 'str'
为什么 Python 会将字符串 "<stdin>" 与匹配该文件名的文件混淆?如果遇到未处理的异常,我不希望 Python 试图从我的磁盘中读取任何文件。
您也可以使用"<string>" 文件名获取它:
$ echo "pining for the fjords" > "<string>"
$ python -c 'wat'
Traceback (most recent call last):
File "<string>", line 1, in <module>
pining for the fjords
NameError: name 'wat' is not defined
有什么方法可以防止这种行为,还是硬编码到 REPL 中?
【问题讨论】:
-
哦,这是一个可爱的错误。你应该举报。