Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NameError: name 'self' is not defined Why? [duplicate]

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.

like image 676
global_singeing Avatar asked May 06 '26 07:05

global_singeing


1 Answers

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

like image 67
Ignacio Vazquez-Abrams Avatar answered May 08 '26 21:05

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!