When my class doesn't override __slots__
, I can get the names and values of all the members of an instance of the class. For example:
class Animal(object):
#__slots__ = ('legs','name','color','smell','age','kids')
def __init__(self):
self.legs = 2
self.name = 'Dog'
self.color= 'Spotted'
self.smell= 'Alot'
self.age = 10
self.kids = 0
a = Animal()
for x in vars(a).items():
print(x[0],x[1])
And the output is
age 10
legs 2
name Dog
kids 0
smell Alot
color Spotted
However, if I uncomment the __slots__ = ...
line in the code above and run it, I will get an error that says vars() argument must have __dict__ attribute
.
Is there anyway I can get the names and values of all the members when __slots__
is overridden?
Aucun commentaire:
Enregistrer un commentaire