Python:未绑定(bind)方法

标签 python

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

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()

此外,如果这是显而易见的并且不值得发布/重新发布,我很抱歉。这是我第一次真正投入到 python 中,我为此工作了一整天,感觉有点傻,现在是凌晨 12 点 30 分。

最佳答案

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

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

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

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

关于Python:未绑定(bind)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6591610/

相关文章:

python - 字典:恢复/删除设置默认值

c++ - 可能的内存泄漏

python - 导入一个简单的 Tensorflow frozen_model.pb 文件并在 C++ 中进行预测

python - 为什么 re.sub 返回 None ?

Python:字符串操作在函数内部不起作用

python - 为道路修建十字路口时出错

python - 解析sql select语句以获取python中的where子句条件

python - Unicode 与 Alembic 不一致

python - Spark 2.0 DataFrame 初始化可能存在的错误

python - 在 Greenplum(Postgres 8.4)中进行多行更新时跟踪错误记录?