改变对象的字符串显示

# l=list('hello')
#
# print(l)
# file=open('test.txt','w')
# print(file)

class Foo:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def __str__(self):
        return '名字是%s 年龄是%s' %(self.name,self.age)
#
# f1=Foo('egon',18)
# print(f1) #str(f1)--->f1.__str__()
#
# x=str(f1)
# print(x)
#
# y=f1.__str__()
# print(y)



class Foo:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    # def __str__(self):
    #     return '折是str'
    def __repr__(self):
        return '名字是%s 年龄是%s' %(self.name,self.age)

f1=Foo('egon',19)
#repr(f1)---->f1.__repr__()
print(f1) #str(f1)---》顺序:f1.__str__()没有str的时候:------>f1.__repr__()

 

相关文章:

  • 2022-01-10
  • 2021-11-29
  • 2021-08-19
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-11
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案