首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过列表调用类函数

通过列表调用类函数
EN

Stack Overflow用户
提问于 2013-10-11 13:02:15
回答 3查看 85关注 0票数 1

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

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

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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)

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-11 13:39:57

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

代码语言:javascript
运行
复制
self.update_group = [New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]

而且效果很好。我不知道附加到数组中的问题是什么,但是有一个固定的列表/数组似乎对我来说很好。@Matthias实际上让我查看了将实例添加到数组的方法。

票数 0
EN

Stack Overflow用户

发布于 2013-10-11 13:06:39

这是一个简单的错误。

代码语言:javascript
运行
复制
self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]

应该是

代码语言:javascript
运行
复制
self.update_group.extend([New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button])

您正在尝试将append索引到应该调用它的位置。第二点是,附加只接受一个参数,所以您应该使用extend

但是您仍然在对类进行操作,而不是对类的实例进行操作。

票数 1
EN

Stack Overflow用户

发布于 2013-10-11 13:10:48

您正在尝试将one分配给one(1),将two分配给two(),这会导致referenced before assignment错误,因此将类名更改为更合适的类,如OneTwo

此外,严格来说,推荐( CapWords约定见pg4 http://msdl.cs.mcgill.ca/people/shahla/misc/PythonConventions.pdf)总是以大写字符启动python类名。

修改后的代码如下(我已经实现了str方法)

代码语言:javascript
运行
复制
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()

输出:

代码语言:javascript
运行
复制
2
3
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19318833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档