常见问题:__str__ vs __repr__ [英] FAQ: __str__ vs __repr__

查看:69
本文介绍了常见问题:__str__ vs __repr__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,但我不知道。我确实在网上搜索,我确实读过了

常见问题,但我太傻了,无法理解。


据我所知,__ ttr__只是

对象的表示。例如:


class ServerConnection:

def __str __(self):

buf =" Server:" + self.name +" \ n"

buf + ="已发送字节:" + str(self.sentBytes)+" \ n"

buf + =" Recv bytes:" + str(self.recvBytes)+" \ n"

返回buf


但是,我不明白__repr__应该是什么。在文档中有一个短语

,这对于初学者来说非常困惑,比如

me:如果可能的话,这应该看起来像一个有效的Python表达式

可用于重新创建具有相同值的对象(给定一个

适当的环境)。那是什么意思?这是否意味着我应该返回


def __str __(自我):

buf =" self.name = " + self.name +" \\\
"

buf + =" self.sentBytes =" + str(self.sentBytes)+" \ n"

buf + =" self.recvBytes =" + str(self.recvBytes)+" \ n"

返回buf


..或者还有一些其他有效的Python表达式我还没有遇到的格式?

Sorry, but I Just Don''t Get It. I did search the ''net, I did read the
FAQ, but I''m too dumb to understand.

As far as I can gather, __str__ is just a representation of the
object. For instance:

class ServerConnection:
def __str__(self):
buf = "Server: " + self.name + "\n"
buf += "Sent bytes: " + str(self.sentBytes) + "\n"
buf += "Recv bytes: " + str(self.recvBytes) + "\n"
return buf

However, I don''t understand what __repr__ should be. There''s a phrase
in the documentation which makes it highly confusing for a beginner like
me: "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).". What does that mean? Does it mean that I
should return:

def __str__(self):
buf = "self.name=" + self.name + "\n"
buf += "self.sentBytes=" + str(self.sentBytes) + "\n"
buf += "self.recvBytes=" + str(self.recvBytes) + "\n"
return buf

..or is there some other "valid Python expression" format which I
have yet to encounter?

推荐答案

Jan Danielssonaécrit:
Jan Danielsson a écrit :
对不起,但我不知道。我确实搜索了网络,我确实读过了常见问题解答,但我太傻了,无法理解。

据我所知,__str__只是代表了对象。


....是的,这个表示是为人眼建造的。如果没有显示您对象中包含的每一点信息

,请不要担心
过多担心。漂亮的打印规则......
Sorry, but I Just Don''t Get It. I did search the ''net, I did read the
FAQ, but I''m too dumb to understand.

As far as I can gather, __str__ is just a representation of the
object.
.... yep, and this representation is built for human eyes. Don''t
worry too much if it does not display every bit of information
contained in your object. Pretty printing rules ...
str(0.1)
0.1 str(" it''一个坏主意)
这是一个坏主意

然而,我不明白__repr__应该是什么。


这是对象内容的*完全*(如果可能)描述,

很好地打包成字符串。

repr(0.1)
0.10000000000000001 repr(这是一个坏主意)
str(0.1) 0.1 str("it''s a bad idea") "it''s a bad idea"
However, I don''t understand what __repr__ should be.
It is an *exact* (if possible) description of the object''s content,
nicely packaged into a string.
repr(0.1) 0.10000000000000001 repr("it''s a bad idea")



''" it 这是一个坏主意''


文档中有一个短语
,这对于像我这样的初学者来说非常混乱:如果可能的话,这应该看起来像一个有效的Python表达式,可以用来重新创建一个具有相同值的对象(给定一个适当的环境)。


''"it\''s a bad idea"''

There''s a phrase
in the documentation which makes it highly confusing for a beginner like
me: "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).".




这意味着等式eval(repr(x))== x应该成立。


干杯,


SB



It means that the equality eval(repr(x)) == x should hold.

Cheers,

SB




勘误表:

Errata:
str(0.1)
''0.1''str(它是一个坏的ide a)
这是一个坏主意

repr(0.1)
''0.10000000000000001''repr(这是个坏主意)
str(0.1) ''0.1'' str("it''s a bad idea") "it''s a bad idea"
repr(0.1) '' 0.10000000000000001'' repr("it''s a bad idea")



''" it'\\'是一个坏主意"''

SB


嗯,这意味着eval(repr(x))== x,如果可能的话。

基本上:

repr(''abc'') - > ''abc''

str(''abc'') - > abc


你会注意到''abc''是字符串的有效python表达式,

而abc不是有效的字符串表达式。


Andreas


2005年6月15日星期三02:46:04 PM +0200,Jan Danielsson写道:
Well, It means that eval(repr(x)) == x if at all possible.
Basically:
repr(''abc'') -> ''abc''
str(''abc'') -> abc

You''ll notice that ''abc'' is a valid python expression for the string,
while abc is not a valid string expression.

Andreas

On Wed, Jun 15, 2005 at 02:46:04PM +0200, Jan Danielsson wrote:
抱歉,但我只是没有得到它。我确实搜索了网络,我确实读过了常见问题解答,但我太傻了,无法理解。

据我所知,__str__只是代表了对象。例如:

类ServerConnection:
def __str __(self):
buf =" Server:" + self.name +" \\\
"
buf + ="已发送字节:" + str(self.sentBytes)+" \ n"
buf + =" Recv bytes:" + str(self.recvBytes)+" \\\
"
返回buf

然而,我不明白__repr__应该是什么。文档中有一个短语
,这对于像我这样的初学者来说非常混乱:如果可能的话,这应该看起来像一个有效的Python表达式
可以使用重新创建具有相同值的对象(给定一个适当的环境)。那是什么意思?这是否意味着我应该返回:

def __str __(自我):
buf =" self.name =" + self.name +" \\\
"
buf + =" self.sentBytes =" + str(self.sentBytes)+" \ n"
buf + =" self.recvBytes =" + str(self.recvBytes)+" \\\
"
返回buf

..还有其他一些有效的Python表达式我尚未遇到的格式?
-
http://mail.python.org/mailman/listinfo/python-list
Sorry, but I Just Don''t Get It. I did search the ''net, I did read the
FAQ, but I''m too dumb to understand.

As far as I can gather, __str__ is just a representation of the
object. For instance:

class ServerConnection:
def __str__(self):
buf = "Server: " + self.name + "\n"
buf += "Sent bytes: " + str(self.sentBytes) + "\n"
buf += "Recv bytes: " + str(self.recvBytes) + "\n"
return buf

However, I don''t understand what __repr__ should be. There''s a phrase
in the documentation which makes it highly confusing for a beginner like
me: "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).". What does that mean? Does it mean that I
should return:

def __str__(self):
buf = "self.name=" + self.name + "\n"
buf += "self.sentBytes=" + str(self.sentBytes) + "\n"
buf += "self.recvBytes=" + str(self.recvBytes) + "\n"
return buf

..or is there some other "valid Python expression" format which I
have yet to encounter?
--
http://mail.python.org/mailman/listinfo/python-list



这篇关于常见问题:__str__ vs __repr__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆