Are used to convert objects into string output
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return ("str full name :%s Age %s" % (self.name, self.age))
# def __repr__(self):
# return ("repr full name :%s Age %s" % (self.name, self.age))
if __name__ == '__main__':
f = Foo(' Zhang Fei ', '22')
print(f) # Equivalent to calling str(f), It is also equivalent to calling the class __str()__ Method
Output results :
str full name : Zhang Fei Age 22
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
# def __str__(self):
# return ("str full name :%s Age %s" % (self.name, self.age))
def __repr__(self):
return ("repr full name :%s Age %s" % (self.name, self.age))
if __name__ == '__main__':
f = Foo(' Zhang Fei ', '22')
print(f) # Equivalent to calling str(f), It is also equivalent to calling the class __str()__ Method
Output results :
repr full name : Zhang Fei Age 22