Pythonの__str__と__repr__
コメントもらったので試してみた。
point.py
"""Point class This is the Point class """ class Point: __x = 0.0 __y = 0.0 z = 0.0 def __init__(self, x1, y1, z1) : self.__x = x1 self.__y = y1 self.z = z1 def toString(self) : return '(' + str(self.__x) + ', ' + str(self.__y) + ', ' + str(self.z) + ')' def __str__(self) : return '<' + str(self.__x) + ', ' + str(self.__y) + ', ' + str(self.z) + '>' def __repr__(self) : return '{' + str(self.__x) + ', ' + str(self.__y) + ', ' + str(self.z) + '}'
>>> import point >>> p=point.Point(1,2,3) >>> help(point) Help on module point: NAME point - Point class FILE /Users/Shared/point.py DESCRIPTION This is the Point class CLASSES Point class Point | Methods defined here: | | __init__(self, x1, y1, z1) | | __repr__(self) | | __str__(self) | | toString(self) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | z = 0.0 (END) >>> p {1, 2, 3} >>> print p <1, 2, 3> >>> str(p) '<1, 2, 3>' >>> repr(p) '{1, 2, 3}'
参考: