I am right now studying Python classes and running into a thing which book doesn't explain much.
I have a code :
class Human(object):
def __init__(self,*args,**kwargs):
self.first_name = kwargs.setdefault('first')
self.last_name = kwargs.setdefault('last')
self.height = kwargs.setdefault('height')
self.weight = kwargs.setdefault('weight')
def bmi(self):
return self.weight/float(self.height)**2
Human.bmi = bmi
def human_str(self):
return '{} {}'.format(self.first_name, self.last_name)
Human.__str__ = human_str
me = Human(first='Seth', last='Gibson')
adam_mechtley = Human(first='Adam', last='Mechtley', weight = 78, height = 1.85)
sis = Human(first='Ruth',last='Gibson')
babysis = Human(first='Emily',last='Gibson')
print(sis)
#Prints : proper name
print(sis,babysis)
#Prints : <place in memory>
I understand overall what's happening , but the question is, why when I print only one instance, str() attribute works, but if I try to print two instances at the same time, repr() attribute is being used?
Thanks for your time!