2016-09-25 9 views
-1

Python 2.7では、非常に大きな整数のカウンターとして機能するクラスZillionを作成しています。私はそれが詰まっていると信じていますが、私はTypeError: 'int' object is not callableに走り続けています。これは、コードのある時点で、私がintを関数のように呼び出そうとしたことを意味するようです。私がこのサイトで見つけた多くの例は、作者がオペレータを省略した数学的なエラーに過ぎませんでした。私は自分の誤りを見つけることができない。クラス '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 
+1

エラーは何行発生しますか?コード・ダンプは、エラーの発生場所を教えてくれないと有用ではありません。私たちは(最後の最新の呼び出し)すべての情報 – CDspace

+0

トレースバックせずにエラーをシミュレートしようとするつもりはない: z.incrementで ファイル「」、ライン1、() はTypeError:「int型のオブジェクトではありません呼び出し可能 私はそれをトップマンに置きます。 –

+0

'z'は整数でなければなりません。' Zillion'クラスのインスタンスではありません。あなたが定義したトレースバックからは分かりません。 – martineau

答えて

1

あなたはインスタンス変数と、少なくともそのトレースバックであなたの問題のようですincrementという名前のメソッドの両方を持っています。 __init__

あなたはself.increment = 1を定義し、そのマスク

はちょうどそれらのいずれかの名前を変更(およびそれが変数名をだならば、あなたはit--を使用するすべての場所を変更することを確認し、修正するには、同じ名前のメソッドincrementメソッド全体のように)

ここで起こっていることを確認する1つの方法は、調査するのにtypeを使用することです。たとえば:だから

def increment(self): 
    […] 

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

あなたは同じ名「Zillion.increment() `とメソッドを定義した後Zillion.__init__()

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

でインスタンス変数を定義しています次のようにメソッドを呼び出すとします:

big_number = Zillion() 
big_number.increment() 

.ìncrementは、.__init__()で定義した整数であり、メソッドではありません。

0

可変メンバself.incrementがあり、__init__関数の1に設定されているためです。

z.incrementはあなたがincrementから_increment(または任意の他の名前)にあなたの関数の名前を変更することができ、それが作品の意志1.

に設定さ可変部材を表しています。

関連する問題