How to print all object properties in one like something like:
print obj
but this does not print all properties of the object
How to print all object properties in one like something like:
print obj
but this does not print all properties of the object
you can use builtin method dir(obj)
The default dir()
mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
If the object is a module object, the list contains the names of the module’s attributes. If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases. Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
source : https://docs.python.org/2/library/functions.html#dir
You can add build-in function to your object (e.g __str__(
) or __repr__()
)
class MyObject:
def __init__(self):
#add proprieties
self.x = 32
self.y = 43
def __str__(self):
return ("Object: " + str(self.x) + " " + str(self.y))