设为首页 加入收藏

TOP

2.从print到自省(三)
2018-11-08 02:08:24 】 浏览:276
Tags:print 自省
th additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a repr() method.

class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
Return a string version of object. If object is not provided, returns the empty string. Otherwise, the behavior of str() depends on whether encoding or errors is given, as follows.

object.__repr__(self)
由repr()内建函数和调用以计算一个对象的“正式”的字符串表示。如果可能的话,这应该看起来像一个有效的Python表达式,可用于重新创建具有相同值的对象(给定适当的环境)。如果不可能,应该返回<...some useful description...>形式的字符串。返回值必须是一个字符串对象。如果一个类定义了__repr__()但没有定义__str__(),那么在请求该类的实例的“非正式”的字符串表示时也将调用__repr__()。

object.__str__(self)
由str(object)、内建函数format()和print()语句调用,以计算一个对象的“非正式的”或可打印的字符串表示。返回值必须是一个字符串对象。
与object.__repr__()不同的是,str()不需要返回一个合法的Python表达式:可以使用更合适或者精确的表示。
内建类型object默认的实现是调用object.__repr__()。

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.
Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目