从模块调用类函数?

发布于 2024-12-06 11:37:31 字数 1106 浏览 3 评论 0原文

我只是为 pygame 编写一些虚拟代码。

第一个代码示例在 menus.py 文件中有一个函数。我想练习使用导入。这很好用。然后我想将该函数放入一个类中,以便我可以启动并运行类。这是第二个代码块。不幸的是,第二个代码块没有运行。有人可以解释一下我哪里出了问题吗?

# menus.py
def color_switcher(counter, screen):
    black = ( 0, 0, 0)
    white = (255, 255, 255)
    green = (0, 255, 0)
    red = (255, 0, 0)

    colors = [black, white, green, red]
    screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.color_switcher(counter, screen)
     #more stuff

这很好用。

这不可以

# menus.py
class Menu:

    def color_switcher(self, counter, screen):
        black = ( 0, 0, 0)
        white = (255, 255, 255)
        green = (0, 255, 0)
        red = (255, 0, 0)

        colors = [black, white, green, red]
        screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu.color_switcher(counter, screen)
     #more stuff

#TypeError: unbound method color_switcher() must be called with Menu instance as first argument (got int instance instead)

有人告诉我我在课堂上做错了什么吗?

I am just writing some dummy code for pygame.

The first sample of code has a function in the menus.py file. I wanted to practice using import. This works fine. I then wanted to put the function in a class so I can get up and running with classes. This is the second block of code. Unfortunately the second block of code doesn't run. Could someone explain where I am going wrong please.

# menus.py
def color_switcher(counter, screen):
    black = ( 0, 0, 0)
    white = (255, 255, 255)
    green = (0, 255, 0)
    red = (255, 0, 0)

    colors = [black, white, green, red]
    screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.color_switcher(counter, screen)
     #more stuff

This works fine.

This doesn't

# menus.py
class Menu:

    def color_switcher(self, counter, screen):
        black = ( 0, 0, 0)
        white = (255, 255, 255)
        green = (0, 255, 0)
        red = (255, 0, 0)

        colors = [black, white, green, red]
        screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu.color_switcher(counter, screen)
     #more stuff

#TypeError: unbound method color_switcher() must be called with Menu instance as first argument (got int instance instead)

Could someone tell me what I am doing wrong with the class please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绮筵 2024-12-13 11:37:31

这不是 import 的问题。由于 color_switcher 不是静态方法,因此您必须首先创建类实例,然后才调用成员函数:

if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu().color_switcher(counter, screen)

或者,您可以将您的类声明为

class Menu:
    @staticmethod
    def color_switcher(counter, screen):

,然后将其用作menus.Menu.color_switcher (柜台,屏幕)

That is not problem with import. Since color_switcher is not static method, you must first create class instance, and only then call a member function:

if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu().color_switcher(counter, screen)

Alternatively, you can declare your class as

class Menu:
    @staticmethod
    def color_switcher(counter, screen):

and then use it as menus.Menu.color_switcher(counter, screen)

孤云独去闲 2024-12-13 11:37:31

然后我想将该函数放入一个类中,以便我可以使用类启动并运行

它并不那么简单。

你真的真的真的需要一个完整的 Python 教程来展示如何进行面向对象编程。

您很少调用类的方法。很少。

您创建一个类的实例(一个对象)并调用该对象的方法。不是班级。对象。

x = Menu()
x.color_switcher(counter, screen)

I then wanted to put the function in a class so I can get up and running with classes

It's not that simple.

You really, really, really need to do a complete Python tutorial that shows how to do object-oriented programming.

You rarely call a method of a class. Rarely.

You create an instance of a class -- an object -- and call methods of the object. Not the class. The object.

x = Menu()
x.color_switcher(counter, screen)
貪欢 2024-12-13 11:37:31

您试图将实例方法作为类方法调用。

两种解决方案:
1) 更改客户端代码:在类的实例上调用方法

menus.Menu().color_switcher(counter, screen) # note the parentheses after Menu

2) 更改定义:使用 类方法注解

You're trying to call an instance method as a class method.

Two solutions:
1) change the client code: call the method on an instance of the class

menus.Menu().color_switcher(counter, screen) # note the parentheses after Menu

2) change the definition: change the instance method to a class method using the class method annotation

吹泡泡o 2024-12-13 11:37:31

您需要先创建 Menu 的实例,然后才能调用该方法。例如:

my_menu = Menu()
my_menu.color_switcher(counter, screen)

您当前将 color_switcher 视为类方法

You need to create an instance of Menu before you can call the method. For example:

my_menu = Menu()
my_menu.color_switcher(counter, screen)

You are currently treating color_switcher as if it is a class method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文