__format__()方法

  __format__()传参方法:someobject.__format__(specification)

  specification为指定格式,当应用程序中出现"{0:specification}".format(someobject)或format(someobject, specification)时,会默认以这种方式调用

  当specification为" "时,一种合理的返回值是return str(self),这为各种对象的字符串表示形式提供了明确的一致性

  注意,"{0!s}".format()和"{0!r}".format()并不会调用__format__()方法,他们会直接调用__str__()或者__repr__()

  例:自定义我们自己的__format__()格式

#coding=utf-8

class formatest:
def __init__(self, name, age):
self.name,self.age = name, age def __format__(self,specification):
if specification == "":
return str(self) strformat = specification.replace("%s",self.name).replace("%r",self.age)
return strformat if __name__ == "__main__":
people = formatest("zhanglin", "")
print ("{}".format(people))
print ("{0:%s-%r}".format(people))
print (format(people, "%s-%r"))

  例:格式化对象中的集合

#coding=utf-8

class people:
def __init__(self, name, age):
self.name,self.age = name, age def __format__(self,specification):
if specification == "":
return str(self) strformat = specification.replace("%s",self.name).replace("%r",self.age)
return strformat class hand:
def __init__(self, *people):
self.peoples = []
self.peoples= list(people)
def __format__(self, specification):
if specification == "":
return str(self)
return ",".join("{:{fs}}".format(c, fs=specification) for c in self.peoples) if __name__ == "__main__":
handobj = hand(people("zhangsan", ""), people("lisi", ""), people("zhanglin", ""))
print ("{:%s-%r}".format(handobj))

Python基本特殊方法之__format__的更多相关文章

  1. python内置方法

    1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...

  2. python之魔法方法介绍

    1.1. 简介 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加“魔法”的特殊方法. 它们经常是两个下划线包围来命名的(比如 __init__ , __lt__ ) ...

  3. python的魔术方法大全

    在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”(魔术方法),例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,这 ...

  4. Python测试函数的方法之一

    Python测试函数的方法之一 首先介绍简单的try......except尝试运行的放例如下面的图和代码来简单介绍下: 注释:提醒以下代码环境为2.7.x 请3.x以上的同学们老规矩print(把打 ...

  5. 使用python原生的方法实现发送email

    使用python原生的方法实现发送email import smtplib from email.mime.text import MIMEText from email.mime.multipart ...

  6. Python中sorted()方法

    Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...

  7. python类及其方法

    python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...

  8. Python内置方法的时间复杂度(转)

    原文:http://www.orangecube.net/python-time-complexity 本文翻译自Python Wiki本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Pyt ...

  9. Python LOGGING使用方法

    Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...

随机推荐

  1. dojo 官方翻译 dojo/aspect

    官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...

  2. Ubuntu启动文件破坏启动恢复方法

    reboot后主机登录显示如下图: 解决步骤: 1.fs0:(回车) 2.edit startup.nsh 3.添加下面字段: fs0: cd EFI/ubuntu grubx64.efi 4.重启即 ...

  3. shiro的Realm

    public class UserRealm extends AuthorizingRealm { private UserService userService = new UserServiceI ...

  4. Sybase:SAP IQ学习笔记

    Sybase:SAP IQ学习笔记 -- 启动IQ管理 >> start_iq -n utility_db -n utility_db >> dbisql -c "u ...

  5. mysql 进阶查询(学习笔记)

    学习笔记,来源:实验楼 ,链接: https://www.shiyanlou.com/courses/9   一.日期计算: 1.要想确定每个宠物有多大,可以使用函数TIMESTAMPDIFF()计算 ...

  6. 1163: [Baltic2008]Mafia

    1163: [Baltic2008]Mafia Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 123  Solved: 70[Submit][Stat ...

  7. 《Maven实战》第6章 仓库

    6.1什么是Maven仓库? Maven仓库:存储所有Maven项目共享的构件的统一位置. Maven仓库的作用:Maven项目仅需声明依赖坐标,即可在需要的时候自动根据坐标找到仓库中的构件. 6.2 ...

  8. Spring_配置 Bean(2)

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  9. 调用webservices中 枚举类型没有被序列化问题

    引用服务后,代理类为自动为所有枚举类型生成了一个Bool类型相关字段,命名方式:比如枚举类名为“PayType”,生成的相关字段为“PayTypeSpecified”,此字段有何作用? PayType ...

  10. spark学习10(win下利用Intellij IDEA搭建spark开发环境)

    第一步:启动IntelliJ IDEA,选择Create New Project,然后选择Scala,点击下一步,输入项目名称wujiadong.spark继续下一步 第二步:导入spark-asse ...