Making an object return its string when called

This question already has an answer here: Difference between __str__ and __repr__? 21 answers 我相信你正在寻找的是__repr__方法。

调用时让对象返回它的字符串

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 我相信你正在寻找的是__repr__方法。

A quest for html.unescape("&nbsp")

This question already has an answer here: Difference between __str__ and __repr__? 21 answers By default, printing containers like tuples , lists and others will use the repr of their items. (In CPython, it was chosen to not implement <container>.__str__ and instead let object.__str__ fill its slot. The __str__ of object will then call tuple.__repr__ which then proceeds to call the re

寻求html.unescape(“&nbsp”)

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 默认情况下,打印容器(如tuples , lists等)将使用其项目的repr 。 (在CPython中,它选择不实现<container>.__str__ ,而是让object.__str__填充它的槽,然后__str__ object调用tuple.__repr__ ,然后继续调用它所包含元素的repr 。 3140)。) 实际上,使用转义码(例如xa0 )为repr调用一个字符串将不会转义它们: print(repr(a)) 'hom

Difference between variable and print variable

This question already has an answer here: Difference between __str__ and __repr__? 21 answers In Python, print shows the result of __str__ on its arguments, which may be different from __repr__ . For more, see here: Difference between __str__ and __repr__ in Python it is beacause of differences between __str__ and __repr__. . diff. str and repr

变量和打印变量之间的区别

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 在Python中, print在其参数上显示__str__的结果,这可能与__repr__不同。 更多,请看这里:Python中__str__和__repr__的区别 这是因为之间的差异 __str__ 和 __repr__。 。 差异。 str和repr

working of \n in python

This question already has an answer here: Difference between __str__ and __repr__? 21 answers There are two functions that give an object's string representation, repr() and str() . The former is designed to convert the object to as-code string, while the latter gives user-friendly string. When you input the variable name in the command line, repr() is used, and n character is shown a

在Python中工作\ n

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 有两个函数可以给出对象的字符串表示repr()和str() 。 前者旨在将对象转换为as-code字符串,而后者则提供用户友好的字符串。 在命令行中输入变量名称时,会使用repr() , n字符显示为n (as-code)。 当你使用print ,使用str() , n显示为一个新行(用户友好)。 顺便说一下, str是一个变量的错误名称,因为它与内置的相同。 当你这样做 st

Angle brackets in Python

This question already has an answer here: Difference between __str__ and __repr__? 21 answers 当应用于Field实例时, repr对__repr__使用以下定义,它是非Python语法的来源。 def __repr__(self): return "<Field (%s).%s>" % (",".join(x.__name__ for x in self.owners),self.name)

Python中的尖括号

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 当应用于Field实例时, repr对__repr__使用以下定义,它是非Python语法的来源。 def __repr__(self): return "<Field (%s).%s>" % (",".join(x.__name__ for x in self.owners),self.name)

Adding a new line character to a variable in python

This question already has an answer here: Difference between __str__ and __repr__? 21 answers '22n4444n666666n88888888n' is the correct string representation of the expected result. In order to actually process the newline characters you need to print it: print EvenLadder(6)

在python中添加一个新的行字符到一个变量

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 '22n4444n666666n88888888n'是预期结果的正确字符串表示形式。 为了实际处理换行符,您需要打印它: print EvenLadder(6)

The difference between

This question already has an answer here: Difference between __str__ and __repr__? 21 answers __str__ and __repr__ are both methods for getting a string representation of an object. __str__ is supposed to be shorter and more user-friendly, while __repr__ is supposed to provide more detail. Specifically, for many data types, __repr__ returns a string that, if you pasted it back into Python

和...之间的不同

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 __str__和__repr__都是获取对象的字符串表示的方法。 __str__应该更短,更方便用户,而__repr__应该提供更多的细节。 具体而言,对于许多数据类型, __repr__返回一个字符串,如果您将其粘贴回Python,它将是一个有效的表达式,其值将等于原始值。 例如, str('Hello')返回'Hello' ,但repr('Hello')返回"'Hell

str() vs repr() functions in python 2.7.5

This question already has an answer here: Difference between __str__ and __repr__? 21 answers When should i use str() and when should i use repr() ? Almost always use str() when creating output for end users. repr() is mainly useful for debugging and exploring. For example, if you suspect a string has non printing characters in it, or a float has a small rounding error, repr() will show

str()vs repr()函数在python 2.7.5中

这个问题在这里已经有了答案: __str__和__repr__之间的区别? 21个答案 什么时候应该使用str()以及何时应该使用repr()? 在为最终用户创建输出时,几乎总是使用str() 。 repr()主要用于调试和探索。 例如,如果您怀疑某个字符串中有非打印字符,或者float具有较小的舍入错误,则repr()将显示您; str()可能不会。 repr()也可以用于生成粘贴到源代码中的文字。 它也可以用于持久化(使用ast.literal_eval或eval

Python how to use the

I've stated programming in Phyton just few days ago, so I'm sorry if the question is quite easy. I've written this code: >>> class Polynomial: def __init__(self, coeff): self.coeff=coeff def __repr_(self): return str(self) def __str__(self): s='' flag=0; for i in reversed(range(len(self.coe

Python如何使用

我前几天在Phyton说过编程,所以如果问题很简单,我很抱歉。 我写了这段代码: >>> class Polynomial: def __init__(self, coeff): self.coeff=coeff def __repr_(self): return str(self) def __str__(self): s='' flag=0; for i in reversed(range(len(self.coeff))): if(i==0): s+= '+

Bring namedtuple's

https://mail.python.org/pipermail/python-ideas/2014-September/029310.html I always thought namedtuple builtin __str__ and __repr__ were very neat and I'm looking for a simple way to apply it to any classes of mine easily. >>> from collections import namedtuple >>> A = namedtuple("A", ["foo"]) >>> print(A(foo=1)) A(foo=1) >>> str(A(foo=1)) 'A(foo=1)' >

带上名字的组合

https://mail.python.org/pipermail/python-ideas/2014-September/029310.html 我一直以为namedtuple内置__str__和__repr__都非常工整,我在寻找一个简单的方法来很容易应用到我的任何类。 >>> from collections import namedtuple >>> A = namedtuple("A", ["foo"]) >>> print(A(foo=1)) A(foo=1) >>> str(A(foo=1)) 'A(foo=1)' >>> repr(A(foo=1)) 'A(foo=1)' 编辑: 我