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

Question:

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.

Asked By: global_singeing

||

Answers:

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

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.