7

How to print all object properties in one like something like:

print obj

but this does not print all properties of the object

Blurry Script
  • 329
  • 1
  • 3
  • 11

2 Answers2

10

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

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
2

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))
Gwendal Grelier
  • 526
  • 1
  • 7
  • 21