Python

Code:

class Rationa:
    def _init_ (self, num, den=1):
        self.num = num;
        self.den = den;

    def print(self):
        print(str(self.num), '/', str(self.den))

R1 = Rationa(1,2)
R1.print()

Error:

Traceback (most recent call last):
  File "D:/Coding/PyPractice/class1.py", line 9, in <module>
    R1 = Rationa(1,2)
TypeError: object() takes no parameters

Solve:

python类的构造函数写错了,init两边应有两个下划线,而不是一个。正确的应为__init__

评论

This is just a placeholder img.