__str__&__repr__

object.__str__(self):

  Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object. This differs from __repr__() in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object.

object.__repr__(self):

  Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form<...some useful description...> should be returned. The return value must be a string object. If a class defines__repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

参考:http://docs.python.org/2.7/reference/datamodel.html?highlight=__mro__

__str__&__repr__的更多相关文章

  1. python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法

    一:string相关:__str__(),__repr__(),__format__() str方法更面向人类阅读,print()使用的就是str repr方法更面对python,目标是希望生成一个放 ...

  2. day29 类中的内置函数方法 __str__ __repr__ __call__ isinstance() issubclass()

    __str__()__repr__()__len__() str() 转字符串repr() 让字符原形毕露的方法len() 计算长度 内置的方法很多,但是并不是全部都在object中,比如len(), ...

  3. python 中的 %s,%r,__str__,__repr__

    1.%s,%r的区别 在进行格式化输出时,%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别. %s ⇒ str(),比较智能: %r ⇒ repr(),处理较 ...

  4. python基础---- __getattribute__----__str__,__repr__,__format__----__doc__----__module__和__class__

    目录: 一. __getattribute__ 二.__str__,__repr__,__format__ 三.__doc__ 四.__module__和__class__ 一. __getattri ...

  5. Python的程序结构[1] -> 方法/Method[4] -> 魔术方法 __call__ / __str__ / __repr__

    __call__ 方法 __call__ 是当对象被调用时会调用的方法,允许一个对象(类的实例等)像函数一样被调用,也可以传入参数. 1 class Foo(): 2 def __init__(sel ...

  6. __str__,__repr__,__format__

    __str__,__repr__ __str__:控制返回值,并且返回值必须是str类型,否则报错 __repr__:控制返回值并且返回值必须是str类型,否则报错 __repr__是__str__的 ...

  7. 反射,内置方法,__str__ __repr__

    反射 反射用到的mmp模块 def wahaha():print('wahaha') class QQxing: def __init__(self,name): self.name = name d ...

  8. __str__,__repr__,__add__

    class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def __ ...

  9. 复习python的__call__ __str__ __repr__ __getattr__函数 整理

    class Www: def __init__(self,name): self.name=name def __str__(self): return '名称 %s'%self.name #__re ...

随机推荐

  1. [Luogu4630][APIO2018]Duathlon 铁人两项

    luogu 题目描述 比特镇的路网由 \(m\) 条双向道路连接的 \(n\) 个交叉路口组成. 最近,比特镇获得了一场铁人两项锦标赛的主办权.这场比赛共有两段赛程:选手先完成一段长跑赛程,然后骑自行 ...

  2. salesforce 公开网页,免登陆

    設定→カスタマイズ→コミュニテ→新建一个visfualforce的コミュニテ ビルド->カスタマイズ->コミュニティ->すべてのコミュニティ->新規コミュニティ 然后选中新规的 ...

  3. Nomad 了解

    Introduction to Nomad Welcome to the intro guide to Nomad! This guide is the best place to start wit ...

  4. Django ORM哪些操作

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...

  5. 【转】HP laserjet p2055dn的自动双面打印功能

    原文网址:http://zhidao.baidu.com/link?url=n_NW7Qfa_7HlrEhLucdvKO43jj3SpFXJhGAfQ-WqF979jm80eUv8s1atqtxE7w ...

  6. win7环境下,golang thrift demo代码编译不通过

    用官方的教程代码:http://thrift.apache.org/tutorial/go 用网友提供的代码:Golang RPC 之 Thrift 都出现如下情况 状况1: 编辑器中就会提醒 Can ...

  7. CentOS部署NetCore - 3. 部署站点

    一. CentOS 安装FTP linux 安装 ftp第一步 使用如下命令#rpm -qa |grep vsftpd可以检测出是否安装了vsftpd软件, 如果没有安装,使用YUM命令进行安装. y ...

  8. CentOS 7 named设置主从复制

    前两篇文章介绍了named的安装和配置forward. 本文将介绍named的主从复制. 在从named的配置中添加: zone "weiheng.ink" IN { type s ...

  9. Nginx启停

    启动nginx /usr/local/nginx/nginx #不指定配置文件地址/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx. ...

  10. pyc是什么

    python是解释型语言,需要解释器对程序逐行做出解释,然后直接运行. C语言是编译型语言,PC不需要翻译,直接执行就可以了. java也是解释型语言,不过速度可以跟编译型媲美. 用java举例,ja ...