Search code examples
pythonpycharmself

Unresolved reference in class with "self" keyword


I'm new at Python so I'm trying to create a class with some attributes inside of it. However, Pycharm gave me an Unresolved reference with the self usage. Here is my code:

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)

I'm not sure what am I missing in here. Why my self keyword doesn't work properly?


Solution

  • This worked for me. (Proper Indentation)

    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)