+5 votes
by (100 points)

I was attempting to place certain class variables within a list. Each class has a method known as update and considering they each do the exact same thing I though it would be allot more convenient to simply place them in a list and call the method that way.

The method I am trying to call is known as update and this is the error that I get:

Traceback (most recent call last):
  File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 177, in <module>
    initialize_game()
  File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 61, in initialize_game
    start_menu(debugging, screen, clock, image_cache)
  File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 88, in __init__
    self.init_start_screen()
  File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 115, in init_start_screen
    self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]
TypeError: 'builtin_function_or_method' object is not subscriptable

Here is the code I am trying to use

Entities.py:

class Quit_Game_Button(Entity):

    def __init__(self, x, y, image_cache):
        Entity.__init__(self)
        self.image_cache = image_cache
        self.image = function.get_image("images/Quit_Game.png", self.image_cache)
        self.image.convert()
        self.rect = Rect(x, y, 150, 30)

    def update(self, mouse_position):
        if self.rect.collidepoint(mouse_position):
            self.image = function.get_image("images/Quit_Game_Hover.png", self.image_cache)
        else:
            self.image = function.get_image("images/Quit_Game.png", self.image_cache)

    def check_click(self, clicked, mouse_position):
        quit = False
        if self.rect.collidepoint(mouse_position):
            if clicked:
                quit = True
        return quit

class Settings_Button(Entity):

    def __init__(self, x, y, image_cache):
        Entity.__init__(self)
        self.image_cache = image_cache
        self.image = function.get_image("images/Settings_Button.png", self.image_cache)
        self.image.convert()
        self.rect = Rect(x, y, 50, 50)

    def update(self, mouse_position):
        if self.rect.collidepoint(mouse_position):
            self.image = function.get_image("images/Settings_Button_Hover.png", self.image_cache)
        else:
            self.image = function.get_image("images/Settings_Button.png", self.image_cache)

main.py

self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]

mouse_position = pygame.mouse.get_pos()

#Updating Entities on the screen
for entity in self.update_group:
    entity.update(mouse_position)

Just so it's clear, the functions do exist. I was just wondering if I was going about calling methods the write way through a list/array? If not could someone please point me in the right direction? :)

6 Answers

+14 votes
by (100 points)

You are trying to assign one to one(1) and two to two() , that results in a referenced before assignment error So change the class Names to something more suitable like One and Two

Also It is strictly recommended (for CapWords convention see pg4 here ) to always start a python class name with a uppercase character

Corrected code would look like: (I've implemented the str method)

class One:
    def __init__(self, x):
        self.x = x

    def update(self):
        self.x += 1

    def __str__(self):
        return str(self.x)

class Two:
    def __init__(self, x):
        self.x = x

    def update(self):
        self.x += 1

    def __str__(self):
        return str(self.x)

def test():
    one = One(1)
    two = Two(2)

    update_list = [one, two]

    for i in update_list:
        i.update()
        print i

test()

Output:

2
3
+14 votes
by (100 points)

This is a simple error.

self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]

should be

self.update_group.extend([New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button])

You're trying to index append where you should call it. Second point is, that append takes only one parameter, so you should use extend .

But you're still operating on the classes, not instances of the classes.

+12 votes
by (100 points)

The issue did not lie within the actual for loop for calling the update function within the class, it occurred through the appending off the class instances to the list. Therefore to solve this problem I tried:

self.update_group = [New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]

and it worked perfectly. I don't know what the issue is with the appending to the array, but having a fixed list/array seems to work just fine for me. Credits to @Matthias for actually making me look at the method for adding the instances to the array.

+8 votes
by (100 points)

It's smarter to use Regular Expressions for this sort of thing. To find instances of a substring in a string:

var str = 'Greetings, SomeKittens, how are you, SomeKittens?';
var nameCount = str.match(/SomeKittens/g).length;
+2 votes
by (100 points)

use group_concat(type) as type in your code. (if it is wrong sry. pls explain it clear )

0 votes
by (100 points)

FYI -- Constraint Layout alpha 9 added Chains , which allow you to implement this behavior.

enter image description here

Related questions

...