通过列表调用类函数

时间:2013-10-11 13:02:16

标签: python function

我试图在列表中放置某些类变量。每个类都有一个称为更新的方法,并且考虑到它们各自执行完全相同的操作,但是将它们简单地放在列表中并以这种方式调用方法会更方便。

我试图调用的方法称为更新,这是我得到的错误:

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

以下是我尝试使用的代码

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)

很明显,功能确实存在。我只是想知道我是否打算通过列表/数组调用方法?如果没有,请有人指出我正确的方向? :)

3 个答案:

答案 0 :(得分:1)

这是一个简单的错误。

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

应该是

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

您正试图将append编入索引。第二点是,append只需要一个参数,因此你应该使用extend

但你仍在操作类,而不是类的实例。

答案 1 :(得分:1)

您正尝试将one分配给one(1)two分配给two(),这会导致referenced before assignment错误,因此请将班级名称更改为更多名称适合像OneTwo

严格 recommended (对于CapWords惯例,请参阅第4页here始终以大写字符

启动python类名

更正的代码如下所示:(我已实施 str 方法)

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()

输出:

2
3

答案 2 :(得分:0)

问题不在于在类中调用更新函数的实际for循环中,它是通过将类实例附加到列表中而发生的。因此,我尝试解决这个问题:

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

它完美无缺。我不知道附加到数组的问题是什么,但是有一个固定的列表/数组似乎对我来说很好。归功于@Matthias实际让我看看将实例添加到数组的方法。

相关问题