简体   繁体   English

即使代码正确执行,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()

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:考虑到上面的代码,在 VSCODE 中运行,我想知道如何摆脱所有继续弹出的“pylint”错误,这些错误提示类似于以下内容:

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!谢谢你的时间!

Rather than suppress the error message, why not fix the code?为什么不修复代码,而不是抑制错误消息? Turtle presents two APIs, a functional one and an object-oriented one. Turtle 提供了两种 API,一种是功能性的,一种是面向对象的 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.分析工具无法查看源库文件内部并查看功能签名。

Since you're defining your own Polygon object , I don't see why you're not using the object-oriented interface to turtle.由于您正在定义自己的Polygon object ,我不明白您为什么不使用面向对象的turtle 接口。 The import I use below blocks the functional interface and only allows access to the object-oriented one:我在下面使用的import阻止了功能接口,只允许访问面向对象的接口:

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()

Note the subtle changes to the code to accommodate the object-oriented API.注意代码的细微变化以适应面向对象的 API。 Now try your analysis of the code to see if this solves your problem.现在尝试分析代码,看看这是否能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM