【问题标题】:TypeError: unbound methodTypeError:未绑定的方法
【发布时间】:2013-12-25 14:21:22
【问题描述】:

我正在编写一个程序,通过不同的方法将数字从一个基转换为另一个基,并且我正在使用类和模块。

模块 #1 包含具有属性值和基础的类 Number 模块 #2 包含没有属性的控制器类 模块 #3 包含具有属性 ctrl 的类 Console

class Console:
     def __init__(self, ctrl):
         self.__ctrl = ctrl

     def __successiveDivisions(self):
         n1 = self.__readNo("Enter the number: ")
         b1 = self.__readBase("Enter the base of the number: ")
         b2 = self.__readBase("Enter the base in which to convert the number: ")
         n2 = self.__ctrl.successiveDivisions(n1, b1, b2) 

*self.__ctrl.successiveDivisions(n1,b1,b2)* 返回 number 类型的对象

为什么会出现这个错误?

   TypeError: unbound method successiveDivisions() must be called with Controller instance as first argument (got str instance instead)

我试着写了:

  n2 = Console()
  n2 = self.__ctrl.successiveDivisions(n1, b1, b2) 

但是我收到了这个错误:

 TypeError: __init__() takes exactly 2 arguments (1 given)

我做错了什么?

【问题讨论】:

    标签: class python-2.7 typeerror


    【解决方案1】:

     n2 = Console()
    

    您需要传递您在类定义中声明的参数。这就是你得到的原因

    TypeError: __init__() takes exactly 2 arguments (1 given)
    

    应该是

    n2 = Console(ctrl)
    

    ctrl 应该支持successiveDivisions(n1, b1, b2)。意味着它应该支持带有三个参数的successiveDivisions。这不应该是无限的递归。

    【讨论】:

    • 嗯,这就是问题所在......如果我写 n=Console(ctrl) 我再次得到第一个错误。我应该进行哪些更改才能使其正常工作?
    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2014-04-15
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多