__str__&__repr__
【__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__的更多相关文章
- python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法
一:string相关:__str__(),__repr__(),__format__() str方法更面向人类阅读,print()使用的就是str repr方法更面对python,目标是希望生成一个放 ...
- day29 类中的内置函数方法 __str__ __repr__ __call__ isinstance() issubclass()
__str__()__repr__()__len__() str() 转字符串repr() 让字符原形毕露的方法len() 计算长度 内置的方法很多,但是并不是全部都在object中,比如len(), ...
- python 中的 %s,%r,__str__,__repr__
1.%s,%r的区别 在进行格式化输出时,%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别. %s ⇒ str(),比较智能: %r ⇒ repr(),处理较 ...
- python基础---- __getattribute__----__str__,__repr__,__format__----__doc__----__module__和__class__
目录: 一. __getattribute__ 二.__str__,__repr__,__format__ 三.__doc__ 四.__module__和__class__ 一. __getattri ...
- Python的程序结构[1] -> 方法/Method[4] -> 魔术方法 __call__ / __str__ / __repr__
__call__ 方法 __call__ 是当对象被调用时会调用的方法,允许一个对象(类的实例等)像函数一样被调用,也可以传入参数. 1 class Foo(): 2 def __init__(sel ...
- __str__,__repr__,__format__
__str__,__repr__ __str__:控制返回值,并且返回值必须是str类型,否则报错 __repr__:控制返回值并且返回值必须是str类型,否则报错 __repr__是__str__的 ...
- 反射,内置方法,__str__ __repr__
反射 反射用到的mmp模块 def wahaha():print('wahaha') class QQxing: def __init__(self,name): self.name = name d ...
- __str__,__repr__,__add__
class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def __ ...
- 复习python的__call__ __str__ __repr__ __getattr__函数 整理
class Www: def __init__(self,name): self.name=name def __str__(self): return '名称 %s'%self.name #__re ...
随机推荐
- 浅谈ecmall插件机制
插件是独立于原系统的程序模块,目的是在不修改原程序的情况下对系统进行扩展,便于修改和管理.目前web开发中大多是使用钩子形式来定义插件, 比较典型的有 wordpress, drupal系统 ecma ...
- 8.Python编写登录接口
1.python需安装flask,在命令行窗口输入:pip3 install flask 2.代码如下所示: from flask import Flask,request,jsonify,sessi ...
- 7个去伪存真的JavaScript面试题
1.创建JavaScript对象的两种方法是什么? 这是一个非常简单的问题,如果你用过JavaScript的话.你至少得知道一种方法.但是,尽管如此,根据我的经验,也有很多自称是JavaScript程 ...
- centos初始化安装
1.yum 安装 nginx rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6. ...
- Nginx (一)Windows下编译Nginx源码以及安装 nginx for windows方法步骤
转载自: http://apps.hi.baidu.com/share/detail/11192699#content Nginx介绍: Nginx ("engine x")是一个 ...
- Servlet 前端后台交互
一. URL地址传值 1.1. 地址传值 http://localhost:8080/xj/123/name.json servlet 对应接受方法 @RequestMapping(value=& ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- 安装windows 2003 server
最近去给客户安装windows 2003 server,想想也是技术,就写出来了,我这个实在虚拟机安装的,安装这个系统主要是为了安装limesurvey问卷系统 第一步:安装,选择Enter 第二步: ...
- C/C++程序内存情况
一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) — 一 ...
- 【BZOJ】2657: [Zjoi2012]旅游(journey)(树的直径)
题目 传送门:QWQ 分析 在任意两个不相邻的点连一条线,求这条线能穿过几个三角形. 建图比较讲究(详见代码) 求树的直径. 代码 #include <bits/stdc++.h> usi ...