'int' object is not callable

后端 未结 3 1646
北荒
北荒 2021-01-14 04:47

I\'m trying to define a simply Fraction class

And I\'m getting this error:

python fraction.py 
Traceback (most recent call last):
File          


        
3条回答
  •  抹茶落季
    2021-01-14 05:14

    You can't overload the name numerator to refer to both the member variable and the method. When you set self.numerator = n, you're overwriting the reference to the method, and so when you call f.numerator(2), it's trying to do a method call on the member variable, which is an int, and Python doesn't let you do that. It's like saying x = 2; x(4) -- it just doesn't make any sense.

    You should rename the setter methods to set_numerator and set_denominator to remove this naming conflict.

提交回复
热议问题