我想让一个类在实例化时自动记录该类的所有对象,因此,给出这段代码:
class Comet:
comets = []
__init__(self, mass):
self.mass = mass
Comet.comets.append(**instance name as a string**)
HalleysComet = Comet(3*10**14)
Temple 1 = Comet(7.9*10**13)
print(str(Comet.comets))
输出将是:
['HalleysComet', 'Temple 1']