1.repr
object.__ repr__(self)
Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <…some useful description…> should be returned. The return value must be a string object. If a class defines __ repr__() but not __ str__(), then __ repr__() is also used when an “informal” string representation of instances of that class is required.
This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
被repr()函数调用来输出对象的正式的字符串表示,通常情况下,这种表示应该尽可能像有效的python表达式; 此方法的返回值必须是个字符串;如果类中定义了repr但没有定义str, 那需要非正式的字符串表示时repr也会被使用; repr方法通常用于调试.
对象在解释器中的显示内容就是通过repr来控制的.
见下例
>>>class Test:
def __repr__(self):
return 'hahaha'
t = Test()
>>>t
hahaha
>>>class Test1:
pass
t1 = Test1()
>>>t1
<Test1 object at 0x000001D7BE8B3668>
2.str
object.str(self)
Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.
This method differs from object.repr() in that there is no expectation that str() return a valid Python expression: a more convenient or concise representation can be used.
The default implementation defined by the built-in type object calls object.repr().
被str(), format(), print()函数调用来输出对象的 ‘非正式’ 字符串表示; 返回值必须是个字符串; 和repr不同的是不要求返回一个有效的python表达式, 所以返回一个方便清晰的字符串是个不错的选择. 内置类型对象默认调用repr来显示.
所以print()函数显示内容调用的是str方法返回的结果
class Test2:
def __str__(self):
return 'hahaha'
def __repr__(self):
return 'enenen'
t2 = Test2()
>>>print(t2)
hahaha
>>>t2
enenen
3.format
object.__ format__(self, format_spec)
1)Called by the format() built-in function, and by extension, evaluation of formatted string literals and the str.format() method, to produce a “formatted” string representation of an object. The format_spec argument is a string that contains a description of the formatting options desired. The interpretation of the format_spec argument is up to the type implementing __ format__(), however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax.
2)See Format Specification Mini-Language for a description of the standard formatting syntax.
The return value must be a string object.
类的内置format方法被format()函数,以及通过扩展,评估格式化的字符串文字的str.format()方法调用, 来生成对象的格式化字符串显示; format_spec参数是一个字符串,包含所需的格式化选项的说明;,该方法必须返回一个字符串.
但是大多数类会将格式化委托给其中一个内置类型,或者使用类似的格式化选项语法。(不是很懂)
下例为format的一个使用示例:
format_dic = {'123': '{0.year}{0.month}{0.day}',
'321': '{0.day}{0.month}{0.year}'}
class Foo:
def __format__(self, format_spec):
print('format运行')
if not format_spec or format_spec not in format_dic:
format_spec = '123'
f = format_dic[format_spec]
return f.format(self)
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
d = Foo('2018', '12', '20')
print(format(d, '321'))
#result
format运行
20122018