Search code examples
pythonindentationnameerror

Python NameError: name 'self' is not defined Why?


At the top:

import pygame, sys
from pygame.sprite import Sprite
from pygame.locals import *
pygame.init()

Part not working:

class DetectionBox(Sprite):
    def __init__(self):
        Sprite.__init__(self)
        self.img = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT/4], SRCALPHA, 32).convert_alpha()
        self.pos = (0, SCREEN_HEIGHT - (SCREEN_HEIGHT/4)*3)
DETECT_BOX = DetectionBox()

Error: NameError: name 'self' is not defined

Someone please explain why this isn't working, because I have no clue. It's working properly with every other class, so it's something about this one.


Solution

  • You've goofed up your indentation, mixing spaces and tabs. Use python -tt to verify.