从模块调用类函数?
我只是为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是
import
的问题。由于 color_switcher 不是静态方法,因此您必须首先创建类实例,然后才调用成员函数:或者,您可以将您的类声明为
,然后将其用作menus.Menu.color_switcher (柜台,屏幕)
That is not problem with
import
. Sincecolor_switcher
is not static method, you must first create class instance, and only then call a member function:Alternatively, you can declare your class as
and then use it as
menus.Menu.color_switcher(counter, screen)
它并不那么简单。
你真的真的真的需要一个完整的 Python 教程来展示如何进行面向对象编程。
您很少调用类的方法。很少。
您创建一个类的实例(一个对象)并调用该对象的方法。不是班级。对象。
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.
您试图将实例方法作为类方法调用。
两种解决方案:
1) 更改客户端代码:在类的实例上调用方法
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
2) change the definition: change the instance method to a class method using the class method annotation
您需要先创建 Menu 的实例,然后才能调用该方法。例如:
您当前将 color_switcher 视为类方法。
You need to create an instance of Menu before you can call the method. For example:
You are currently treating color_switcher as if it is a class method.