zoukankan      html  css  js  c++  java
  • 自定制格式化方式format

    自定制格式化方式format

    # x='{0}{0}{0}'.format('dog')
    #
    # print(x)
    
    # class Date:
    #     def __init__(self,year,mon,day):
    #         self.year=year
    #         self.mon=mon
    #         self.day=day
    # d1=Date(2016,12,26)
    #
    # x='{0.year}{0.mon}{0.day}'.format(d1)
    # y='{0.year}:{0.mon}:{0.day}'.format(d1)
    # z='{0.mon}-{0.day}-{0.year}'.format(d1)
    # print(x)
    # print(y)
    # print(z)
    
    # x='{0.year}{0.mon}{0.day}'.format(d1)
    # y='{0.year}:{0.mon}:{0.day}'
    # z='{0.mon}-{0.day}-{0.year}'
    
    format_dic={
        'ymd':'{0.year}{0.mon}{0.day}',
        'm-d-y':'{0.mon}-{0.day}-{0.year}',
        'y:m:d':'{0.year}:{0.mon}:{0.day}'
    }
    class Date:
        def __init__(self,year,mon,day):
            self.year=year
            self.mon=mon
            self.day=day
        def __format__(self, format_spec):
            print('我执行啦')
            print('--->',format_spec)
            if not format_spec or format_spec not in format_dic:
                format_spec='ymd'
            fm=format_dic[format_spec]
            return fm.format(self)
    d1=Date(2016,12,26)
    # format(d1) #d1.__format__()
    # print(format(d1))
    print(format(d1,'ymd'))
    print(format(d1,'y:m:d'))
    print(format(d1,'m-d-y'))
    print(format(d1,'m-d:y'))
    print('===========>',format(d1,'asdfasdfsadfasdfasdfasdfasdfasdfasdfasdfasdfasdfasd'))
  • 相关阅读:
    mysqlnd cannot connect to MySQL 4.1+ using old authentication
    nginx中查看关于php的配置和php-fpm的重启等操作
    nginx中查看关于php的配置和php-fpm的重启等操作
    linux面试题1
    linux笔试
    面试题
    shell脚本实例
    shell脚本
    mysql主从复制
    Discuz!NT静态文件缓存(SQUID)
  • 原文地址:https://www.cnblogs.com/jiawen010/p/10135508.html
Copyright © 2011-2022 走看看