__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. iOS clang 编译 oc 代码

    clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iP ...

  2. pt-osc测试

    pt-osc测试 1.原表必须存在主键 PRIMARY KEY 或者 UNIQUE KEY The new table `darren`.`_t_user_new` does not have a P ...

  3. EasyUI:datagrid控件简介

    EasyUI:datagrid控件简介 1,水平滚动条属性: //显示滚动条 fitColumns:false //不显示滚动条 fitColumns:true

  4. yum安装mysql后root用户的临时密码

    1.查看root用户临时随机密码 yum 安装mysql后,无法通过空密码登录数据库,如下: [root@ mysql]# mysql -u root -p Enter password: ERROR ...

  5. v4l2中的多流机制

    一直在搞camera,对v4l2也比较熟悉了,今天写文章说点自己的看法 对于v4l2,对多流的支持是比较弱的,只是一个流.但现在的camera 硬件,能支持多个流同事工作,所以又必要对v4l2的api ...

  6. 搭建 Spring 开发环境

    把以下 jar 包加入到工程的 classpath 下: Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC ...

  7. redhat6.8链路聚合

    centos 6.X   聚合链路 0.查看NetworkManager服务,停止NetworkManager服务.不做这一步很可能出问题          service NetworkManage ...

  8. Mybatis常见问题

    1.#和$的区别和联系 1.1#是占位符,会对Sql进行预编译,相当于?:$是做Sql拼接,有sql注入的隐患 1.2#不需要关注数据类型,Mybatis自动实现类型转换,$必须自己判断数据类型联系 ...

  9. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  10. 《Computational Statistics with Matlab》硬译

    第1章 从随机变量采样 研究者提出的概率模型对于分析方法来说通常比较复杂,研究者处理复杂概率模型时越来越依赖计算.数值方法,通过使用计算方法,研究者就不用对一些分析技术做一些不现实的假设(如正态性和独 ...