我真的不明白Python的用途__str__和__repr__用途。我的意思是,我得到的__str__返回对象的字符串表示形式。但是我为什么需要那个?在什么用例情况下?另外,我读到有关__repr__
但是我不明白的是,我将在哪里使用它们?
Answers:
由
repr()内置函数和字符串转换(反引号)调用以计算对象的“正式”字符串表示形式。如果可能的话,这应该看起来像一个有效的Python表达式,可用于重新创建具有相同值的对象(在适当的环境下)。
由
str()内置函数和print语句调用以计算对象的“非正式”字符串表示形式。
__str__每当您将此类用作字符串的一部分时使用,如果您有一个类,并且想要提供信息/非正式的输出。例如,您可以定义__str__Django模型的方法,然后在Django管理界面中对其进行呈现。<Model object>您将得到一个人的名字和姓氏,事件的名称和日期等信息,而不是类似的信息。
__repr__和__str__是相似的,实际上有时等于(实施例从BaseSet类中sets.py从标准库):
def __repr__(self):
"""Return string representation of a set.
This looks like 'Set([<list of elements>])'.
"""
return self._repr()
# __str__ is the same as __repr__
__str__ = __repr__
__repr__代替__str__Django使用吗?
经常使用它们的一个地方是互动会话。如果打印对象,则将__str__调用其方法,而如果仅使用对象,__repr__则显示该对象:
>>> from decimal import Decimal
>>> a = Decimal(1.25)
>>> print(a)
1.25 <---- this is from __str__
>>> a
Decimal('1.25') <---- this is from __repr__
的__str__目的是尽可能使人可读,而的__repr__目的应该是可以用来重新创建对象的内容,尽管在这种情况下,它通常不是创建对象的确切方式。
两者__str__并__repr__返回相同的值(对于内置类型肯定是这样)也很常见。
__repr__,官方文档并没有真正回答您为什么要使用它的问题,而仅仅因为在其他地方回答了一个问题,也就不是一个很好的理由。这样就可以回答了(如果您不认为这个问题值得回答,那么您通常就不会回谷歌了!)如果您认为这个问题不值得回答,那么您就无法回答,尽管在这种情况下,我同意它已经很好地涵盖了因此,链接到重复项将是一个适当的响应。
建立在先前的答案之上,并显示更多示例。如果使用正确,则str和之间的区别repr很明显。总之repr应返回的字符串,可以是复制粘贴到重建对象的确切状态,而str为有用logging和observing调试的结果。以下是一些示例,以查看某些已知库的不同输出。
print repr(datetime.now()) #datetime.datetime(2017, 12, 12, 18, 49, 27, 134411)
print str(datetime.now()) #2017-12-12 18:49:27.134452
可以将str其打印到日志文件中,repr如果要直接运行该日志文件或将其作为命令转储到文件中,可以将其重新用途。
x = datetime.datetime(2017, 12, 12, 18, 49, 27, 134411)
print repr(np.array([1,2,3,4,5])) #array([1, 2, 3, 4, 5])
print str(np.array([1,2,3,4,5])) #[1 2 3 4 5]
在Numpy中,它又repr可以直接消费。
class Vector3(object):
def __init__(self, args):
self.x = args[0]
self.y = args[1]
self.z = args[2]
def __str__(self):
return "x: {0}, y: {1}, z: {2}".format(self.x, self.y, self.z)
def __repr__(self):
return "Vector3([{0},{1},{2}])".format(self.x, self.y, self.z)
在此示例中,repr再次返回可以直接使用/执行的字符串,而str作为调试输出更为有用。
v = Vector3([1,2,3])
print str(v) #x: 1, y: 2, z: 3
print repr(v) #Vector3([1,2,3])
要记住的一件事是,如果
str未定义repr,str则会自动调用repr。因此,至少定义一个总是好的repr
让我们拥有一个没有__str__功能的类。
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
emp1 = Employee('Ivan', 'Smith', 90000)
print(emp1)
当我们打印类的实例时,emp1我们得到的是:
<__main__.Employee object at 0x7ff6fc0a0e48>
这不是很有帮助,并且如果我们使用它来显示(例如在html中),当然这不是我们想要打印的内容
所以现在,同一个类,但是具有__str__功能:
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
def __str__(self):
return(f"The employee {self.first} {self.last} earns {self.pay}.")
# you can edit this and use any attributes of the class
emp2 = Employee('John', 'Williams', 90000)
print(emp2)
现在,不用打印一个对象,而是使用__str__函数的返回值获取指定的内容:
The employee John Williams earns 90000
str将是非正式且可读的格式,而repr将提供正式的对象表示。
class Complex:
# Constructor
def __init__(self, real, imag):
self.real = real
self.imag = imag
# "official" string representation of an object
def __repr__(self):
return 'Rational(%s, %s)' % (self.real, self.imag)
# "informal" string representation of an object (readable)
def __str__(self):
return '%s + i%s' % (self.real, self.imag)
t = Complex(10, 20)
print (t) # this is usual way we print the object
print (str(t)) # this is str representation of object
print (repr(t)) # this is repr representation of object
Answers :
Rational(10, 20) # usual representation
10 + i20 # str representation
Rational(10, 20) # repr representation
str和repr都是表示的方式。您可以在编写课程时使用它们。
class Fraction:
def __init__(self, n, d):
self.n = n
self.d = d
def __repr__(self):
return "{}/{}".format(self.n, self.d)
例如,当我打印它的实例时,它返回东西。
print(Fraction(1, 2))
结果是
1/2
而
class Fraction:
def __init__(self, n, d):
self.n = n
self.d = d
def __str__(self):
return "{}/{}".format(self.n, self.d)
print(Fraction(1, 2))
也导致
1/2
但是,如果您同时编写了两者,那么python使用哪个呢?
class Fraction:
def __init__(self, n, d):
self.n = n
self.d = d
def __str__(self):
return "str"
def __repr__(self):
return "repr"
print(Fraction(None, None))
这导致
str
因此,当两者都编写时,python实际上使用str方法而不是repr方法。