Pylint Error with Python Turtle even though code executes properly

北战南征 提交于 2021-02-11 12:09:50

问题


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:

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!


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/62841377/pylint-error-with-python-turtle-even-though-code-executes-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!