python 2.7.5中的str()vs repr()函数 [英] str() vs repr() functions in python 2.7.5
问题描述
python 2.7.5中str()
和repr()
函数之间的区别是什么?
关于python.org的解释:
str()
函数用于返回相当人类可读的值的表示形式,而repr()
则用于生成 可以由解释器读取的表示形式(如果没有等效语法,则强制为SyntaxError
)
但是我不清楚.
一些例子:
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'" # repr is giving an extra double quotes
>>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285' # repr is giving value with more precision
所以我想知道以下内容
- 何时应使用
str()
,何时应使用repr()
? - 在哪种情况下我可以使用其中任何一个?
-
str()
不能执行repr()
做什么? -
repr()
不能做什么?
我什么时候应该使用str()和什么时候应该使用repr()?
为最终用户创建输出时,几乎总是使用str()
.
repr()
主要用于调试和探索.例如,如果您怀疑一个字符串中没有打印字符,或者一个浮点数有一个小的舍入错误,则repr()
将显示给您; str()
可能不会.
repr()
在生成要粘贴到源代码中的文字时也很有用.它也可以用于持久性(与ast.literal_eval
或eval
一起使用),但这不是一个好主意-如果您想要可编辑的持久性值,则JSON或YAML之类的东西会更好,并且您不打算要对其进行编辑,请使用泡菜.
2.在哪种情况下,我可以使用其中任何一个?
好吧,您可以在几乎任何地方使用它们.除上述内容外,您通常不应该使用它们.
3.
str()
不能执行repr()
做什么?
让您输出适合最终用户使用的信息-并非总是如此(例如,str(['spam','eggs'])不太可能是您要放在GUI中的任何东西),但更常见的是比repr()
.
4.
repr()
不能做什么>>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" # repr is giving an extra double quotes >>> str(1.0/7.0) '0.142857142857' >>> repr(1.0/7.0) '0.14285714285714285' # repr is giving value with more precision
再次提供对调试有用的输出-并非总是如此(用户创建的类的实例的默认值很少有帮助),但会尽可能地提供.
有时甚至会为您提供有效的Python文字或其他表达式的输出-但除了交互式探索之外,您几乎不想依赖它.
what is the difference between str()
and repr()
functions in python 2.7.5?
Explanation on python.org:
The
str()
function is meant to return representations of values which are fairly human-readable, whilerepr()
is meant to generate representations which can be read by the interpreter (or will force aSyntaxError
if there is no equivalent syntax)
But it wasn't clear for me.
some examples:
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'" # repr is giving an extra double quotes
>>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285' # repr is giving value with more precision
so I want to know the following
- When should I use
str()
and when should I userepr()
? - In which cases I can use either of them?
- What can
str()
do whichrepr()
can't? - What can
repr()
do whichstr()
can't?
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 you; str()
may not.
repr()
can also be useful for generating literals to paste into your source code. It can also be used for persistence (with ast.literal_eval
or eval
), but this is rarely a good idea--if you want editable persisted values, something like JSON or YAML is much better, and if you don't plan to edit them, use pickle.
2.In which cases i can use either of them ?
Well, you can use them almost anywhere. You shouldn't generally use them except as described above.
3.What can
str()
do whichrepr()
can't ?
Give you output fit for end-user consumption--not always (e.g., str(['spam', 'eggs']) isn't likely to be anything you want to put in a GUI), but more often than repr()
.
4.What can
repr()
do whichstr()
can't
Give you output that's useful for debugging--again, not always (the default for instances of user-created classes is rarely helpful), but whenever possible.
And sometimes give you output that's a valid Python literal or other expression--but you rarely want to rely on that except for interactive exploration.
这篇关于python 2.7.5中的str()vs repr()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!