pycharm 类中具有“self”关键字的未解析引用

vm0i2vca  于 2022-11-08  发布在  PyCharm
关注(0)|答案(1)|浏览(449)

我是Python新手,所以我尝试创建一个类,里面有一些属性。然而,Pycharm给了我一个Unresolved引用,里面有self用法。下面是我的代码:

from turtle import Turtle

class Pad:
    def __init__(self, is_player):
        self.is_player = is_player

    pad_body = Turtle()
    pad_body.penup()
    pad_body.shape("square")
    pad_body.shapesize(4, 1, 0)
    pad_body.color("white")

    if self.is_player:  # this is the place I get the error
        pad_body.setposition(-480, 0)
    else:
        pad_body.setposition(480, 0)

我不确定我在这里遗漏了什么。为什么我的self关键字不能正常工作?

vsdwdz23

vsdwdz231#

这对我很有效。(适当的缩进)

from turtle import Turtle

class Pad:
    def __init__(self, is_player):
        self.is_player = is_player

        pad_body = Turtle()
        pad_body.penup()
        pad_body.shape("square")
        pad_body.shapesize(4, 1, 0)
        pad_body.color("white")

        if self.is_player:  # this is the place I get the error
            pad_body.setposition(-480, 0)
        else:
            pad_body.setposition(480, 0)

相关问题