TypeError:未绑定的方法

时间:2013-12-08 14:03:22

标签: class python-2.7 typeerror

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

模块#1包含具有属性值和基础的类Number 模块#2包含没有属性的Controller类 模块#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)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

 n2 = Console()

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

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

应该是

n2 = Console(ctrl)

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