While I was skimming over this article of printing out all instances of a class: Printing all instances of a class I came across a major problem. I wanted a solution that works without having to import anything but when I tried one of the solutions:
class A:
instances = []
def __init__(self):
self.__class__.instances.append(self)
print('\n'.join(A.instances)) #this line was suggested by @anvelascos
it didn't work.
This is my code:
class Planets:
instances = []
def __init__(self, days, p_type, distance_from_sun):
self.lengthOfDay = days
self.planetType = p_type
self.distanceFromSun = distance_from_sun
self.__class__.instances.append(self)
T = 'Terrestrial'
G = 'Gas Giant'
I = 'Ice Giant'
mercury = Planets(88, T, 0.4)
venus = Planets(225, T, 0.7)
earth = Planets(365, T, 1)
mars = Planets(687, T, 1.5)
jupiter = Planets(4333, G, 5.2)
saturn = Planets(10759, G, 9.5)
uranus = Planets(30687, I, 19.8)
neptune = Planets(60190, I, 30)
print(Planets.instances)
It gives me the message:
[<__main__.Planets object at 0x0000016715003FD0>, <__main__.Planets object at 0x0000016715003F10>, <__main__.Planets object at 0x0000016715003EB0>,
<__main__.Planets object at 0x0000016715003E50>, <__main__.Planets object at 0x0000016715003DF0>, <__main__.Planets object at 0x0000016715003D90>, <__main__.Planets object at 0x0000016715003D30>, <__main__.Planets object at 0x0000016715003CD0>]
Do you know how to get the actual name of the instance and not whatever this is?