python - 通过列表调用类函数

标签 python function

我试图将某些类变量放入列表中。每个类都有一个称为 update 的方法,考虑到它们都做完全相同的事情,我认为将它们简单地放在列表中并以这种方式调用该方法会更方便。

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

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

这是我尝试使用的代码

实体.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)

很明显,这些功能确实存在。我只是想知道我是否要通过列表/数组以写入方式调用方法?如果没有,有人可以指出我正确的方向吗? :)

最佳答案

您尝试将 one 分配给 one(1),将 two 分配给 two(),导致引用前分配错误因此将类名称更改为更合适的名称,例如OneTwo

也是严格 recommended (对于 CapWords 约定,请参阅 pg4 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

关于python - 通过列表调用类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318833/

相关文章:

python - 避免 C 编写的 Python 库的垃圾回收

python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

c - C 中结构内的函数指针

c++ - 将变量传递给函数

python - Pybind11:使用 Pybind11 在 C++ 中转换 numpy 数组的问题

python - Polars 子集结构变量字段名称

python - 如何释放在 SWIG 的自定义构造函数中分配的内存?

c++ - 如何在 C++ 中返回一个有效的迭代器?

JavaScript curry : what are the practical applications?

function - 试图理解 OCaml 中的这段代码