未绑定的方法必须调用实例作为第一个参数(没有改为) [英] unbound method must be called with instance as first argument (got nothing instead)

查看:494
本文介绍了未绑定的方法必须调用实例作为第一个参数(没有改为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动所有的类从一个继承。
我写了这个小脚本:

  class c1:
def move():
x + = 1
y + = 1
class c2(c1):
y = 1
x = 2
c = c2
c.move当我运行它时,打印(str(cx)+,+ str(cy))



我得到:

 跟踪(最近最后调用):文件/home/tor/Workspace/try.py,行9,in< module> 
c.move()TypeError:未绑定的方法move()必须使用c2实例作为第一个参数调用(无任何东西)[完成在0.1s
退出代码1]



我做错了什么?

解决方案

  • 所有方法必须至少包含一个参数,传统上称为 self


  • 字段。您的代码现在修改了该范围中不存在的本地变量。



  • Im trying to move all classes from one Inheritance. I wrote this tiny script:

    class c1:
        def move():
            x+=1
            y+=1
    class c2(c1):
        y=1
        x=2
    c=c2
    c.move()
    print(str(c.x)+" , "+str(c.y))
    

    when i run it i get:

    Traceback (most recent call last):   File "/home/tor/Workspace/try.py", line 9, in <module>
         c.move() TypeError: unbound method move() must be called with c2 instance as first argument (got nothing instead) [Finished in 0.1s
    with exit code 1]
    

    what did I do wrong?

    解决方案

    1. You do not instantiate anything

    2. All methods must take at least one parameter, traditionally called self.

    3. You need self to access object fields. Your code right now modifies local variables which do not exist in that scope.

    这篇关于未绑定的方法必须调用实例作为第一个参数(没有改为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆