GVKun编程网logo

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

6

这篇文章主要围绕python2.7.5中的str和vsrepr展开,旨在为您提供一份详细的参考资料。我们将全面介绍python2.7.5中的str的优缺点,解答vsrepr的相关问题,同时也会为您带来

这篇文章主要围绕python 2.7.5中的strvs repr展开,旨在为您提供一份详细的参考资料。我们将全面介绍python 2.7.5中的str的优缺点,解答vs repr的相关问题,同时也会为您带来c语言中getchar()、putchar()函数例子、hasattr()vs try-except块来处理不存在的属性、inspect.getmembers()vs __dict __items()vs dir()、inspect.getmembers()vs __dict __。items()vs dir()的实用方法。

本文目录一览:

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

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

python 2.7.5str()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

所以我想知道以下

  1. 什么str()时候应该使用repr()?什么时候应该使用?
  2. 在哪种情况下我可以使用它们中的任何一个?
  3. str()做什么不能做repr()
  4. repr()做什么不能做str()

答案1

小编典典

我什么时候应该使用str()和什么时候应该使用repr()?

str()在为最终用户创建输出时几乎总是使用。

repr()主要用于调试和探索。例如,如果您怀疑一个字符串中没有打印字符,或者一个浮点数有一个小的四舍五入错误,repr()它将显示给您;str()不得。

repr()在生成要粘贴到源代码中的文字时也很有用。它也可以用于持久性(使用ast.literal_evaleval),但这不是一个好主意-
如果您想要可编辑的持久性值,则像JSON或YAML之类的更好,如果您不打算对其进行编辑,请使用pickle 。

2.在什么情况下我可以使用它们中的任何一个?

好了,你 可以 在几乎任何地方使用它们。你 不应该 通常使用它们作为上述除。

3.str()做什么不能做repr()

为您提供适合最终用户使用的输出-
并非总是如此(例如,str([‘spam’,’eggs’])不太可能是您要放在GUI中的任何东西),但是比起更多repr()

4.什么可以repr()做哪些str()不可以

再次为您提供对调试有用的输出-并非总是如此(用户创建的类的实例的默认值很少有帮助),但会尽可能输出。

有时会为您提供有效的Python文字或其他表达式的输出-但除了交互式探索之外,您几乎不想依赖它。

c语言中getchar()、putchar()函数例子

c语言中getchar()、putchar()函数例子

1、

#include <stdio.h>

int main(void)
{
    char ch;
    
    ch = getchar();
    while(ch != '\n')
    {
        if(ch == ' ')
        {
            putchar(ch);
        }
        else
        {
            putchar(ch + 1);
        }
        ch = getchar();
    }
    putchar(ch);
    
    return 0;
}

 

hasattr()vs try-except块来处理不存在的属性

hasattr()vs try-except块来处理不存在的属性

if hasattr(obj,'attribute'):
    # do somthing

try:
    # access obj.attribute
except AttributeError,e:
    # deal with AttributeError

应该首选哪个,为什么?

inspect.getmembers()vs __dict __items()vs dir()

inspect.getmembers()vs __dict __items()vs dir()

任何人都可以通过适当的例子向我解释一下黑白之间有什么区别

>>> import inspect
>>> inspect.getmembers(1)

>>> type(1).__dict__.items()

>>> dir(1)

除了它们按顺序显示递减的属性和方法编号外。1是整数(但可以是任何类型。)


编辑

>>>obj.__class__.__name__  #gives the class name of object  
>>>dir(obj)                #gives attributes & methods  
>>>dir()                   #gives current scope/namespace
>>>obj.__dict__            #gives attributes

inspect.getmembers()vs __dict __。items()vs dir()

inspect.getmembers()vs __dict __。items()vs dir()

任何人都可以通过适当的例子向我解释一下黑白之间有什么区别

>>> import inspect>>> inspect.getmembers(1)

>>> type(1).__dict__.items()

>>> dir(1)

除了它们按顺序显示递减的属性和方法编号外。1是整数(但可以是任何类型。)


编辑

>>>obj.__class__.__name__  #gives the class name of object  >>>dir(obj)                #gives attributes & methods  >>>dir()                   #gives current scope/namespace>>>obj.__dict__            #gives attributes

答案1

小编典典

dir()允许您通过定义来自定义对象报告的属性__dir__()

在手册中,如果__dir__()未定义:

如果对象是模块对象,则列表包含模块属性的名称。

如果对象是类型或类对象,则列表包含其属性的名称以及递归其基础属性的名称。

否则,列表将包含对象的属性名称,其类的属性名称以及递归其类的基类的属性。

这也是inspect.getmembers()返回值,除了返回元组(name, attribute)而不是名称。

object.__dict__是形式{key: attribute, key2: atrribute2}等的字典。

object.__dict__.keys() 有其他两个所缺少的。

从以下文档inspect.getmembers()

当参数为类时,getmembers()不会返回元类属性(此行为是从dir()函数继承的)。

对于int.__dict__.keys(),这是

[''__setattr__'', ''__reduce_ex__'', ''__reduce__'', ''__class__'', ''__delattr__'', ''__subclasshook__'', ''__sizeof__'', ''__init__'']

总之,dir()并且inspect.getmembers()基本上是相同的,同时__dict__是完整的命名空间,包括元类的属性。

关于python 2.7.5中的strvs repr的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c语言中getchar()、putchar()函数例子、hasattr()vs try-except块来处理不存在的属性、inspect.getmembers()vs __dict __items()vs dir()、inspect.getmembers()vs __dict __。items()vs dir()等相关知识的信息别忘了在本站进行查找喔。

本文标签: