python(五)——__str__魔法函数

python(五)——__str__魔法函数

class A():
    def __init__(self, list_name):
        self.list_name = list_name

class B():
    def __init__(self, list_name):
        self.list_name = list_name

    def __str__(self):

        return ','.join(self.list_name)

if __name__=='__main__':
    a = A(['c1','c2','c3','c4'])
    print(a) #隐含调用的是print(str(a))
    #<__main__.A object at 0x000001695875E898>
    b = B(['c1','c2','c3','c4'])
    print(b)
    #c1,c2,c3,c4

猜你喜欢

转载自blog.csdn.net/zxq6661/article/details/112797395