使用海龟的 PyCharm 错误语法错误 [英] PyCharm false syntax error using turtle

查看:32
本文介绍了使用海龟的 PyCharm 错误语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码完美运行,但是,PyCharm 抱怨 forward(100)

中的语法错误

#!/usr/bin/python从海龟进口 *前锋(100)完毕()

由于 turtle 是一个标准库,我认为我不需要做额外的配置,对吗?

解决方案

forward() 函数可通过指定 __all__turtle 模块中,相关部分来自源代码:

_tg_turtle_functions = [..., 'forward', ...]__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +_tg_utilities + _math_functions)

目前,pycharm 无法看到模块的 __all__ 列表中列出的对象,因此将它们标记为 unresolved reference.它的 bugtracker 中有一个未解决的问题:

从方法中创建函数:更新 __all__ 以供星号导入使用p>

另请参阅:有人可以用 Python 解释 __all__ 吗?

<小时>

仅供参考,您可以添加 noinspection 注释来告诉 Pycharm 不要将其标记为未解析的引用:

从海龟导入 *#noinspection PyUnresolvedReferences前锋(100)完毕()

或者,禁用特定范围的检查.

<小时>

当然,严格来说,你应该遵循 PEP8避免通配符导入:

进口龟乌龟.前进(100)乌龟.done()

The code below works perfectly, however, PyCharm complains about syntax error in forward(100)

#!/usr/bin/python
from turtle import *

forward(100)

done()

Since turtle is a stanrd library I don't think that I need to do additional configuration, am I right?

解决方案

The forward() function is made available for importing by specifying __all__ in the turtle module, relevant part from the source code:

_tg_turtle_functions = [..., 'forward', ...]
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
           _tg_utilities + _math_functions)

Currently, pycharm cannot see objects being listed in module's __all__ list and, therefore, marks them as an unresolved reference. There is an open issue in it's bugtracker:

Make function from method: update __all__ if existing for starred import usage

See also: Can someone explain __all__ in Python?


FYI, you can add the noinspection comment to tell Pycharm not to mark it as an unresolved reference:

from turtle import *

#noinspection PyUnresolvedReferences
forward(100)

done()

Or, disable the inspection for a specific scope.


And, of course, strictly speaking, you should follow PEP8 and avoid wildcard imports:

import turtle

turtle.forward(100)
turtle.done()

这篇关于使用海龟的 PyCharm 错误语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆