__format__

一、__format__

  • 自定制格式化字符串
date_dic = {
'ymd': '{0.year}:{0.month}:{0.day}',
'dmy': '{0.day}/{0.month}/{0.year}',
'mdy': '{0.month}-{0.day}-{0.year}',
} class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day def __format__(self, format_spec):
# 默认打印ymd的{0.year}:{0.month}:{0.day}格式
if not format_spec or format_spec not in date_dic:
format_spec = 'ymd'
fmt = date_dic[format_spec]
return fmt.format(self) d1 = Date(2016, 12, 29)
print(format(d1))
2016:12:29
print('{:mdy}'.format(d1))
12-29-2016

__formart__的更多相关文章

随机推荐

  1. redhat7.4安装gitlab

    1.参考官方安装指南 https://about.gitlab.com/install/#centos-7 2.遇到的问题 2.1.启动postfix出错 错误内容 Job for postfix.s ...

  2. [题解] [CF 1250J] The Parade

    题面 题目大意: 给定一个 \(n\) , 所有军人的数量均在 \([1, n]\) 给定 \(a_i\) 代表高度为 \(i\) 的军人的个数 你要将这些军人分成 \(k\) 行, 满足下面两个条件 ...

  3. [go]ini配置文件解析

    // config.ini [app] server.port = 8080 name = resk enabled = false time = 10s ;我是一个注释 #mysql数据库配置 [m ...

  4. linux中read,write和recv,send的区别

    linux中read,write和recv,send的区别 1.recv和send函数提供了和read和write差不多的功能.但是他们提供了第四个参数来控制读写操作. int recv(int so ...

  5. Vs code调试Dart语言

    1.下载Dart SDK,地址:Dart Windows下载 安装稳定版本,安装完看看有没有配置系统变量. 2.在VS code中安装Dart和Code Runner插件: 3.如果Dart有乱码输出 ...

  6. mfc通过信号量保证线程同步

    1.声明一个全局handle,记住在cpp里也声明 extern HANDLE uiHandle; 2.创建信号量 uiHandle = CreateSemaphore(NULL,1,1,NULL); ...

  7. 图解 HTTP 笔记(一)——了解 Web 及网络基础

    本章内容:Web 建立在何种技术之上,HTTP 协议如何诞生并发展? 一.Web 基于 HTTP 通信 Web 使用一种名为 HTTP (HyperText Transfer Protocol,超文本 ...

  8. 我的dbtreeview–treeview直接连接数据表_delphi教程

    unit Unit1; interface uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs ...

  9. python多线程使用场景

    python多线程使用场景 如果程序时cpu密集型的,使用python的多线程是无法提升效率的,如果程序时IO密集型的,使用python多线程可以提高程序的整体效率 CPU密集型(CPU-bound) ...

  10. 将SSRF升级为RCE(AWS环境)

    原文:https://generaleg0x01.com/2019/03/10/escalating-ssrf-to-rce/ 查找域名范围: 在枚举客户端的子域名时.找到了子域名[docs] 打开这 ...