简体   繁体   English

“int”对象在类中不可调用错误

[英]'int' object is not callable error in class

In python 2.7, I am writing a class called Zillion , which is to act as a counter for very large integers.在 python 2.7 中,我正在编写一个名为Zillion的类,它用作非常大整数的计数器。 I believe I have it riddled out, but I keep running into TypeError: 'int' object is not callable , which seems to mean that at some point in my code I tried to call an int like it was a function.我相信我已经把它搞砸了,但我一直遇到TypeError: 'int' object is not callable ,这似乎意味着在我的代码中的某个时候我试图调用一个int就像它是一个函数一样。 Many of the examples I found on this site were simply a mathematical error where the writer omitted an operator.我在这个网站上找到的许多例子只是一个数学错误,作者省略了一个运算符。 I can't seem to find my error.我似乎找不到我的错误。

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    z.increment()
TypeError: 'int' object is not callable

My code:我的代码:

class Zillion:
    def __init__(self, digits):
        self.new = []
        self.count = 0 # for use in process and increment
        self.increment = 1 # for use in increment
        def process(self, digits):
            if digits == '':
                raise RuntimeError
            elif digits[0].isdigit() == False:
                if digits[0] == ' ' or digits[0] == ',':
                    digits = digits[1:]
                else:
                    raise RuntimeError
            elif digits[0].isdigit():
                self.new.append(int(digits[0]))
                digits = digits[1:]
                self.count += 1
            if digits != '':
                    process(self, digits)
        process(self, digits)
        if self.count == 0:
            raise RuntimeError

        self.new2 = self.new  # for use in isZero

    def toString(self):
        self.mystring =''
        self.x = 0
        while self.x < self.count:
            self.mystring = self.mystring + str(self.new[self.x])
            self.x += 1
        print(self.mystring)

    def isZero(self):
        if self.new2[0] != '0':
            return False
        elif self.new2[0] == '0':
            self.new2 = self.new2[1:]
            isZero(self)
        return True

    def increment(self):
        if self.new[self.count - self.increment] == 9:
            self.new[self.count - self.increment] = 0
            if isZero(self):
                self.count += 1
                self.new= [1] + self.new
            else:
                self.increment += 1
                increment(self)
        elif self.new[self.count - self.increment] != 9:
            self.new[self.count - self.increment] = self.new[self.count - self.increment] + 1

You have both an instance variable and a method named increment that seems to be your problem with that traceback at least.你有一个实例变量和一个名为increment的方法,这至少似乎是你的回溯问题。

in __init__ you define self.increment = 1 and that masks the method with the same name__init__定义self.increment = 1并用相同的名称屏蔽方法

To fix, just rename one of them (and if it's the variable name, make sure you change all the places that use it--like throughout the increment method)要修复,只需重命名其中一个(如果它是变量名,请确保更改所有使用它的地方 - 就像整个increment方法一样)

One way to see what's happening here is to use type to investigate.查看此处发生的情况的一种方法是使用type进行调查。 For example:例如:

>>> type(Zillion.increment)
<type 'instancemethod'>
>>> z = Zillion('5')
>>> type(z.incremenet)
<type 'int'>

You have defined an instance variable in Zillion.__init__()您已经在Zillion.__init__()定义了一个实例变量

def __init__(self, digits):
        self.new = []
        self.count = 0 
        self.increment = 1 # Here!

Then you defined a method with the same name 'Zillion.increment()`:然后你定义了一个同名的方法 'Zillion.increment()`:

def increment(self):
    […]

So if you try to call your method like this:因此,如果您尝试像这样调用您的方法:

big_number = Zillion()
big_number.increment()

.ìncrement will be the integer you have defined in .__init__() and not the method. .ìncrement将是您在.__init__()定义的整数,而不是方法。

Because you have a variable member self.increment ,and it has been set to the 1 in your __init__ function.因为你有一个变量成员self.increment ,并且它已经在你的__init__函数中设置为 1。

z.increment represents the variable member which set to 1. z.increment表示设置为 1 的变量成员。

You can rename your function from increment to the _increment (or any other names), and it will works.您可以将函数从increment重命名为_increment (或任何其他名称),它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM