zoukankan      html  css  js  c++  java
  • 可视化对象(str/repr/bytes)

    • __repr__ 对应repr(object)这个函数,返回一个可以用来表示对象的可打印字符串
    • __str__ 对应str(object)这个函数,返回一个字符串对象,适合用于print输出
    • __bytes__ 对应bytes(object)这个函数,返回bytes对象
    class B:
        def __init__(self,name):
            self.name = name
            
        def __repr__(self):
            return 'call __repr__ name is {0}'.format(self.name)
        
        def __str__(self):
            return 'call __str__ name is {0}'.format(self.name)
        
        def __bytes__(self):
            return 'call __bytes__ name is {0}'.format(self.name).encode('utf-8')
     b = B('zhaochj') 
    1 b 
    2 call __repr__ name is zhaochj 
    1 print(b)
    2 call __str__ name is zhaochj
    1 str(b)
    2 'call __str__ name is zhaochj'
    1 bytes(b)
    2 b'call __bytes__ name is zhaochj'

      

  • 相关阅读:
    EXCEL自动导出HTML
    亡灵序曲超清
    支持国产动画-唐伯卿和曾小兰
    中国表情
    logging 日志
    datetime库运用
    hashlib 加密
    os2
    python json数据处理
    python操作redis
  • 原文地址:https://www.cnblogs.com/tianshug/p/10921448.html
Copyright © 2011-2022 走看看