即使代码正确执行,Python Turtle 也会出现 Pylint 错误 [英] Pylint Error with Python Turtle even though code executes properly
问题描述
import turtle
class Polygon:
def __init__(self,sides,name,size=100,color='black',line_thickness=3):
self.sides=sides
self.name=name
self.size=size
self.color=color
self.line_thickness=line_thickness
self.interior_angles=(self.sides-2)*180
self.angle=self.interior_angles/self.sides
def draw(self):
turtle.color(self.color)
turtle.pensize(self.line_thickness)
for i in range(self.sides):
turtle.forward(self.size)
turtle.right(180-self.angle)
turtle.done()
square=Polygon(4,'Square')
square.draw()
考虑到上面的代码,在 VSCODE 中运行,我想知道如何摆脱继续弹出的所有pylint"错误,这些错误提示类似于以下内容:
Considering the code above, operating in VSCODE, I am wondering how to get rid of all the 'pylint' errors that continue to pop up which suggest something similar to the following:
Module 'turtle' has no 'color' member (pylint no-member)
虽然代码执行得很好,但继续查看错误行令人不安,我想知道是否有解决方案.感谢您的时间!
Although the code executes just fine, it is unsettling to continue having to look at the error lines and I am wondering if there is a solution to this. Thanks for you time!
推荐答案
与其抑制错误信息,不如修复代码?Turtle 提供了两种 API,一种是功能性 API,一种是面向对象.函数式的派生是在加载时从面向对象的派生.分析工具无法查看源库文件内部并查看功能签名.
Rather than suppress the error message, why not fix the code? Turtle presents two APIs, a functional one and an object-oriented one. The functional one is derived from the object-oriented one at load time. Analysis tools can't look inside the source library file and see the functional signatures.
既然你正在定义你自己的Polygon
object,我不明白你为什么不使用面向对象的接口来连接turtle.我在下面使用的 import
阻塞了功能接口,只允许访问面向对象的接口:
Since you're defining your own Polygon
object, I don't see why you're not using the object-oriented interface to turtle. The import
I use below blocks the functional interface and only allows access to the object-oriented one:
from turtle import Screen, Turtle
class Polygon:
def __init__(self, sides, name, size=100, color='black', line_thickness=3):
self.sides = sides
self.name = name
self.size = size
self.color = color
self.line_thickness = line_thickness
self.interior_angles = (self.sides - 2) * 180
self.angle = self.interior_angles / self.sides
def draw(self):
turtle.color(self.color)
turtle.pensize(self.line_thickness)
for _ in range(self.sides):
turtle.forward(self.size)
turtle.right(180 - self.angle)
screen = Screen()
turtle = Turtle()
square = Polygon(4, 'Square')
square.draw()
screen.exitonclick()
注意代码的细微变化以适应面向对象的 API.现在尝试对代码进行分析,看看这是否能解决您的问题.
Note the subtle changes to the code to accommodate the object-oriented API. Now try your analysis of the code to see if this solves your problem.
这篇关于即使代码正确执行,Python Turtle 也会出现 Pylint 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!