簡體   English   中英

“int”對象在類中不可調用錯誤

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

在 python 2.7 中,我正在編寫一個名為Zillion的類,它用作非常大整數的計數器。 我相信我已經把它搞砸了,但我一直遇到TypeError: 'int' object is not callable ,這似乎意味着在我的代碼中的某個時候我試圖調用一個int就像它是一個函數一樣。 我在這個網站上找到的許多例子只是一個數學錯誤,作者省略了一個運算符。 我似乎找不到我的錯誤。

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

我的代碼:

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

你有一個實例變量和一個名為increment的方法,這至少似乎是你的回溯問題。

__init__定義self.increment = 1並用相同的名稱屏蔽方法

要修復,只需重命名其中一個(如果它是變量名,請確保更改所有使用它的地方 - 就像整個increment方法一樣)

查看此處發生的情況的一種方法是使用type進行調查。 例如:

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

您已經在Zillion.__init__()定義了一個實例變量

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

然后你定義了一個同名的方法 'Zillion.increment()`:

def increment(self):
    […]

因此,如果您嘗試像這樣調用您的方法:

big_number = Zillion()
big_number.increment()

.ìncrement將是您在.__init__()定義的整數,而不是方法。

因為你有一個變量成員self.increment ,並且它已經在你的__init__函數中設置為 1。

z.increment表示設置為 1 的變量成員。

您可以將函數從increment重命名為_increment (或任何其他名稱),它會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM