+ 1
How does one print all the attributes of a class at once without calling for each one seperately?
6 Respostas
+ 7
ClassName.__dict__ or ClassName.__dict__.keys() to be exact
+ 7
@Edgar Thanks, good to know :)
+ 6
Right. But @Edgar, doesn't dir() give you methods only?
+ 1
Actually, for this task calling the built-in dir() function is more correct, instances of immutable types don't have a __dict__ attribute.
+ 1
No, @Kuba, if the object doesn't supply the __dir__ method it basically returns every accessible attribute (methods included, of course).
0
If you want it to be done nicely, add a __str__(self) method to your class and return the string you want to display depending of the value of each attribute. Then doing this will print this string :
print(yourVariableOfClass)