Python object.__repr__(self) should be an expression?

后端 未结 7 1873
名媛妹妹
名媛妹妹 2020-12-22 15:36

I was looking at the builtin object methods in the Python documentation, and I was interested in the documentation for object.__repr__(self). Here\'s what it sa

7条回答
  •  醉梦人生
    2020-12-22 15:36

    It should be a Python expression that, when eval'd, creates an object with the exact same properties as this one. For example, if you have a Fraction class that contains two integers, a numerator and denominator, your __repr__() method would look like this:

    # in the definition of Fraction class
    def __repr__(self):
        return "Fraction(%d, %d)" % (self.numerator, self.denominator)
    

    Assuming that the constructor takes those two values.

提交回复
热议问题