类的特殊成员1

class B:
    def __init__(self):   #类后面加括号就执行init方法
        print("1")

    def __call__(self, *args, **kwargs):  #对象加括号就执行call方法
        print("2")

obj = B()

obj()   #B()()


class C:
    def __init__(self):
        print("12")

    def __int__(self):
        return 1

    def __str__(self):
        return "ceshi"

obj = C()

ob = int(obj)  #对象转换int型
print(ob)

bj = str(obj)    #对象转换str型
# print(bj)  同 print(obj)
原文地址:https://www.cnblogs.com/TKOPython/p/12371498.html