最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

SEO心得admin60浏览0评论
本文介绍了python 2.7.5中的str()vs repr()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

python 2.7.5中str()和repr()函数之间的区别是什么?

关于python的解释:

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:

    The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError 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 use repr()?
  • In which cases I can use either of them?
  • What can str() do which repr() can't?
  • What can repr() do which str() 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 which repr() 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 which str() 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.

    发布评论

    评论列表(0)

    1. 暂无评论