Python:未绑定的方法

时间:2011-07-06 04:37:54

标签: python

当我尝试绑定方法时,出现错误:

Traceback (most recent call last):
  File "pygametest3.py", line 12, in <module>
    render = winback.rend()
TypeError: unbound method rend() must be called with winback instance as first argument (got nothing instead)

这是代码,直到违规点:

import sys, pygame

pygame.init()

class winback:
    """Render the window"""
    def rend(self):
        rendsurf.fill(black)
        rendsurf.blit (landsurf, (landx,landy,640,480))
        screen.blit (rendsurf, (0,0,640,480))
        pygame.display.flip()
render = winback.rend()

另外,如果这显然很明显且不值得发布/重新发布,我很抱歉。这是我第一次真正陷入蟒蛇,我整天都在努力,我感觉有点蠢,而且早上12:30。

1 个答案:

答案 0 :(得分:4)

必须先实例化类,然后才能调用它们的正常方法。

class Winback(object):
  def rend(self):
    ...

winback = Winback()
render = winback.rend()

或者您可以使用@staticmethodBut make it a module-level function instead