0

So I have a list of images to be indexed inside a class.

Code with error:

imgs = [pygame.image.load("myimg1.png"), pygame.image.load("myimg2.png"), pygame.image.load("myimg3.png")]

class ParentClass:
    def __init__(self, imgs, coordinates):
        self.imgs = imgs
        self.index = 0
        self.activeImg = imgs[self.index]
        self.x, self.y = coordinates

class ChildClass(ParentClass):
    def __init__(self, imgs, coordinates):
        super().__init__(imgs, coordinates)
        #more stuff

child = ChildClass(imgs, (100, 100))

Error:

TypeError: 'pygame.Surface' object is not subscriptable

Code without error: (Not what I want)

imgs = [pygame.image.load("myimg1.png"), pygame.image.load("myimg2.png"), pygame.image.load("myimg3.png")]

class ParentClass:
    def __init__(self, imgs, startImg, coordinates):
        self.imgs = imgs
        self.index = 0
        self.activeImg = startImg
        self.x, self.y = coordinates

class ChildClass(ParentClass):
    def __init__(self, imgs, coordinates):
        super().__init__(imgs, imgs[0], coordinates)
        #more stuff

child = ChildClass(imgs, (100, 100))

I don't understand why that error comes in my main code. As far as I'm aware, this list of images should be indexed without any errors. Python seems to be thinking I'm indexing through the pygame.Surface object (clearly not what I'm doing). But on the other hand, when I pass it as a parameter, it runs without any errors (which is what I find quite confusing).

Help will be appreciated. Thanks.

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
PradMaster
  • 63
  • 6
  • 1
    Is this is actual code? And what's the exact error with trace? – Carcigenicate Dec 31 '20 at 16:13
  • This isn't my ACTUAL code, (its hefty) it's just a snippet of it where the error occurs. The error to be very precise it : File "C:\Python\TImage Show\main.py", line 79, in __init__ self.activeImg = imgs[self.index] TypeError: 'pygame.Surface' object is not subscriptable – PradMaster Dec 31 '20 at 16:16
  • 2
    This code doesn't seem to reproduce your problem (although I've only run it in my head), so I'm going to guess that when you're calling `ChildClass` in your actual code, you're either accidentally passing the wrong data, or misinterpreting what type of data you're passing. Double check the creation of `ChildClass`. – Carcigenicate Dec 31 '20 at 16:17
  • 2
    Also double check your call to the `super` constructor to make sure that your haven't mixed up arguments being passed. – Carcigenicate Dec 31 '20 at 16:25
  • 1
    The first snippet runs fine for me –  Dec 31 '20 at 16:33
  • There is no error in this code. The bug is elsewhere in your application. You need to examine all the pieces of code in which a `ChildClass` object is created. The mistake is not in the class, but in the argument list when the object is constructed. Learn how to debug ([Python debugging tips](https://stackoverflow.com/questions/1623039/python-debugging-tips)) – Rabbid76 Dec 31 '20 at 17:06
  • The only subscripting being done is on the imgs reference. Try `print(type(imgs))` or `print(repr(imgs))` to see what type of variable is actually there. – RufusVS Dec 31 '20 at 17:34

0 Answers0