设为首页 加入收藏

TOP

2.从print到自省(二)
2018-11-08 02:08:24 】 浏览:274
Tags:print 自省
<class 'str'> #输出 >>> type(str) <class 'type'> # 输出

??没错,按照python文档,str()是一个内置类型,其形式如下:

class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')

??那么str()返回的是什么?——返回object的字符串版本。如下:

输入:

>>> str('a')
'a'             #输出
>>> str(1)
'1'

2.__repr__

同理所得:

>>> print.__repr__
<method-wrapper '__repr__' of builtin_function_or_method object at 0x005A0120>
>>> print.__repr__.__doc__
'Return repr(self).'
>>> repr('a')
"'a'"
>>> repr(1)
'1'

repr()返回了什么?repr(object)返回一个包含对象可打印表示的字符串。对于许多类型,此函数尝试返回一个字符串,该字符串在传递给eva l()时会产生一个具有相同值的对象,否则该表示是一个用尖括号括起来的字符串,其中包含名称该对象的类型以及经常包括该对象的名称和地址的附加信息。一个类可以通过定义一个__repr__()方法来控制该函数为其实例返回的内容。

引号去了哪儿?

>>> print('hello, world!')
hello, world!

??无论是在交互式解释器运行,还是在程序中运行,print('hello, world!')都没有出现引号,那么引号去了哪里?
??我相信这只是print函数显示的一种打印形式,它设置的就是去掉了引号,仅此而已。其实引号只是起到一种标识的作用,并不是字符串的一部分,可以很容易验证,比如len('abc')的结果是3而不是5。但不能说abc是一个字符串,abc脱离引号''没有意义。顺便说一句,print(repr('a')),为什么就显示出引号(结果为'a')?,这不矛盾啊,因为repr('a')返回的结果是"'a'",而print函数在打印的时候吧外面的双引号去掉,仅此而已,不过,repr函数却使字符串'a'通过repr函数变成了"'a'",即两个单引号也成为了字符串的一部分,字符串长度由1变成3,在“再谈print()、str()、repr()”一节有代码说明。

>>> len('abc')
3

为何引号又出现了?

>>> a='b'
>>> a
'b'
>>> print(a)
b

??print函数直接将a打印出来,它是不包含引号的,据说print语句使用str()函数显示对象,交互解释器调用repr()函数来显示对象,但我无法通过python自省等方式得知。这样?print(repr(object))...。

再谈print、str()、repr()

??前面的比较仍然是不够短兵相接,我想应该更近距离的进行比较或许能更接近理解,一次性的在交互式解释器横向比较怎么样?

输入:

>>> b='a'
>>> type(b)
<class 'str'>
>>> len(b)
1
>>> type(print(b))
a
<class 'NoneType'>
>>> print(b)==None
a
True
>>> id(type(print(b)))
a
1610549680
>>> id(a)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    id(a)
NameError: name 'a' is not defined
>>> len(print(b))
a
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
len(print(b))
TypeError: object of type 'NoneType' has no len()
>>> str(b)
'a'
>>> type(str(b))
<class 'str'>
>>> len(str(b))
1
>>> repr(b)
"'a'"
>>> type(repr(b))
<class 'str'>
>>> len(repr(b))
3

??结合前面的说明,可以更清楚的看到,当给repr传一个字符,那么这个函数返回的是一个带引号的字符,长度也增加了2,这,就是它和str()的区别所在;而type(print(b))的结果为NoneType,则说明print(b)的值是None,但print(b)是占据内存的,但a不占有内存,它只是屏幕显示print结果的一种形式,因为a没有被引用,也没有被定义,这段程序在交互式下和作为脚本运行结果都一样

>>> a==None
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
a==None
NameError: name 'a' is not defined
>>> print(b)==None
a
True

最后的安利

??最后的最后,能安利一波最好了!没错,我就是要安利python的自省机制,这里给出安利的链接python自省指南,为啥不在开头给出?因为无论如何,至少到最后能给大家安排上车,就算读到最后还是不知道笔者在扯啥!呃,为啥三个大于号有的颜色不一致?下面附上相关的文档引用

附录相关文档

repr(object)
Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eva l(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together wi

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分享个最新Python入门到实战flask.. 下一篇python记录_day16 类的成员

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目