__str__&__repr__

时间:2021-09-29 22:53:16

__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:类基本的特殊方法&lowbar;&lowbar;str&lowbar;&lowbar;&comma;&lowbar;&lowbar;repr&lowbar;&lowbar;&comma;&lowbar;&lowbar;hash&lowbar;&lowbar;&comma;&lowbar;&lowbar;new&lowbar;&lowbar;&comma;&lowbar;&lowbar;bool&lowbar;&lowbar;&comma;6大比较方法

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

  2. day29 类中的内置函数方法 &lowbar;&lowbar;str&lowbar;&lowbar; &lowbar;&lowbar;repr&lowbar;&lowbar; &lowbar;&lowbar;call&lowbar;&lowbar; isinstance&lpar;&rpar; issubclass&lpar;&rpar;

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

  3. python 中的 &percnt;s&comma;&percnt;r&comma;&lowbar;&lowbar;str&lowbar;&lowbar;&comma;&lowbar;&lowbar;repr&lowbar;&lowbar;

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

  4. python基础---- &lowbar;&lowbar;getattribute&lowbar;&lowbar;----&lowbar;&lowbar;str&lowbar;&lowbar;&comma;&lowbar;&lowbar;repr&lowbar;&lowbar;&comma;&lowbar;&lowbar;format&lowbar;&lowbar;----&lowbar;&lowbar;doc&lowbar;&lowbar;----&lowbar;&lowbar;module&lowbar;&lowbar;和&lowbar;&lowbar;class&lowbar;&lowbar;

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

  5. Python的程序结构&lbrack;1&rsqb; -&gt&semi; 方法&sol;Method&lbrack;4&rsqb; -&gt&semi; 魔术方法 &lowbar;&lowbar;call&lowbar;&lowbar; &sol; &lowbar;&lowbar;str&lowbar;&lowbar; &sol; &lowbar;&lowbar;repr&lowbar;&lowbar;

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

  6. &lowbar;&lowbar;str&lowbar;&lowbar;&comma;&lowbar;&lowbar;repr&lowbar;&lowbar;&comma;&lowbar;&lowbar;format&lowbar;&lowbar;

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

  7. 反射,内置方法,&lowbar;&lowbar;str&lowbar;&lowbar; &lowbar;&lowbar;repr&lowbar;&lowbar;

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

  8. &lowbar;&lowbar;str&lowbar;&lowbar;&comma;&lowbar;&lowbar;repr&lowbar;&lowbar;&comma;&lowbar;&lowbar;add&lowbar;&lowbar;

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

  9. 复习python的&lowbar;&lowbar;call&lowbar;&lowbar; &lowbar;&lowbar;str&lowbar;&lowbar; &lowbar;&lowbar;repr&lowbar;&lowbar; &lowbar;&lowbar;getattr&lowbar;&lowbar;函数 整理

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

随机推荐

  1. leetcode 137&lbrack;转&rsqb;

    没思路.网上找到的. 1. 将每一个int看成32位数,统计每一位出现的次数对3取余,所以需要开辟一个32大小的数组来统计每一位出现的次数 2. 对第一种思路进行简化,模拟3进制: three two ...

  2. HOLOLENS如何调节屏幕亮度和音量?

    圆环左边的两个是亮度按键,右边的是两个音量按键,值得注意的是,无论是两个音量键还是亮度键,它们都被设置成了一凸一凹,凸的按键为音量/亮度+键,凹为-键,其工业设计可见一斑.

  3. win7开防火墙,允许ping通

  4. Arch tty终端使用中文,Fbterm控制台配置

    参考: http://www.linuxidc.com/Linux/2011-08/40746p2.htm 安装: # pacman -S fbterm fcitx-fbterm # sudo gpa ...

  5. SharePoint 2010 Form Authentication &lpar;SQL&rpar; based on existing database

    SharePoint 2010 表单认证,基于现有数据库的用户信息表 本文主要描写叙述本人配置过程中涉及到的步骤,仅作为參考,不要仅限于此步骤. 另外本文通俗易懂,适合大众口味儿. I. 开启并配置基 ...

  6. python&lowbar;如何使用临时文件

    案例: 某项目中,从传感器中获得采集数据,每收集到1G的数据后做是数据分析,最终只保留数据分析的结果,收集到的数据放在内存中,将会消耗大量内存,我们希望把这些数据放到一个临时的文件中 临时文件不能命名 ...

  7. 远程块存储iSCSI

    /* Border styles */ #table-2 thead, #table-2 tr { border-top-width: 1px; border-top-style: solid; bo ...

  8. 在jsp页面中设置了远程验证,在初始化时必须预先调用一次。

    参考链接:http://code.taobao.org/p/sztaotao/diff/5/trunk/code/src/main/webapp/webpage/modules/sys/roleFor ...

  9. BZOJ 1528 &lbrack;POI2005&rsqb;sam-Toy Cars(优先队列)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1528 [题目大意] 地上最多可以放k个玩具,现在给出需求顺序, 问最少需要去架子上拿几 ...

  10. php将汉字转换为拼音和得到词语首字母&lpar;三&rpar;

    <?php function getfirstchar($s0){ $fchar = ord($s0{0}); if($fchar >= ord("A") and $f ...

相关文章