How to print instances of a class using print() in Python?
class Element: def __init__(self, name, city, population): self.name = name self.city = city self.population = population def __str__(self): return str(self.__class__) + '\n' + '\n'.join(('{} = {}'.format(item, self.__dict__[item]) for item in self.__dict__)) elem = Element('canada', 'tokyo', 321345) print(elem)
Output
name = canada city = tokyo population = 321345