Query explained:
So what I’m looking for here is something like PHP’s print_r function.
This is so I can debug my scripts by seeing what’s the state of the object in question.
How to print all the current properties and values of an object in Python?
To print all the current properties and values of an object you want vars()
mixed with pprint()
:
from pprint import pprint
pprint(vars(your_object))
Answer #2:
You are really mixing together two different things.
Use dir()
, vars()
or the inspect
module to get what you are interested in (I use __builtins__
as an example; you can use any object instead).
>>> l = dir(__builtins__)
>>> d = __builtins__.__dict__
Print that dictionary however fancy you like:
>>> print l
['ArithmeticError', 'AssertionError', 'AttributeError',...
or
>>> from pprint import pprint
>>> pprint(l)
['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'DeprecationWarning',
...
>>> pprint(d, indent=2)
{ 'ArithmeticError': <type 'exceptions.ArithmeticError'>,
'AssertionError': <type 'exceptions.AssertionError'>,
'AttributeError': <type 'exceptions.AttributeError'>,
...
'_': [ 'ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'DeprecationWarning',
...
Pretty printing is also available in the interactive debugger as a command:
(Pdb) pp vars()
{'__builtins__': {'ArithmeticError': <type 'exceptions.ArithmeticError'>,
'AssertionError': <type 'exceptions.AssertionError'>,
'AttributeError': <type 'exceptions.AttributeError'>,
'BaseException': <type 'exceptions.BaseException'>,
'BufferError': <type 'exceptions.BufferError'>,
...
'zip': <built-in function zip>},
'__file__': 'pass.py',
'__name__': '__main__'}
Answer #3:
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
There are many 3rd-party functions out there that add things like exception handling, national/special character printing, recursing into nested objects etc. according to their authors’ preferences. But they all basically boil down to this.
Answer #4:
dir has been mentioned, but that’ll only give you the attributes’ names. If you want their values as well try __dict__.
class O:
def __init__ (self):
self.value = 3
o = O()
Here is the output:
>>> o.__dict__
{'value': 3}
Is there a built-in function to print all the current properties and values of an object?
No. The first answer excludes some kinds of attributes, and the accepted answer shows how to get all attributes, including methods and parts of the non-public API. But there is no good complete built-in function for this.
So the short corollary is that you can write your own, but it will calculate properties and other calculated data-descriptors that are part of the public API, and you might not want that:
from pprint import pprint
from inspect import getmembers
from types import FunctionType
def attributes(obj):
disallowed_names = {
name for name, value in getmembers(type(obj))
if isinstance(value, FunctionType)}
return {
name: getattr(obj, name) for name in dir(obj)
if name[0] != '_' and name not in disallowed_names and hasattr(obj, name)}
def print_attributes(obj):
pprint(attributes(obj))
Problems with other answers
Observe the application of the first answer on a class with a lot of different kinds of data members:
from pprint import pprint
class Obj:
__slots__ = 'foo', 'bar', '__dict__'
def __init__(self, baz):
self.foo = ''
self.bar = 0
self.baz = baz
@property
def quux(self):
return self.foo * self.bar
obj = Obj('baz')
pprint(vars(obj))
only prints:
{'baz': 'baz'}
Because vars
only returns the __dict__
of an object, and it’s not a copy, so if you modify the dict returned by vars, you’re also modifying the __dict__
of the object itself.
vars(obj)['quux'] = 'WHAT?!'
vars(obj)
returns:
{'baz': 'baz', 'quux': 'WHAT?!'}
— which is bad because quux is a property that we shouldn’t be setting and shouldn’t be in the namespace…
Applying the advice in the currently accepted answer (and others) is not much better:
>>> dir(obj)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'bar', 'baz', 'foo', 'quux']
As we can see, dir
only returns all (actually just most) of the names associated with an object.
inspect.getmembers
, mentioned in the comments, is similarly flawed – it returns all names and values.
From class
When teaching I have my students create a function that provides the semantically public API of an object:
def api(obj):
return [name for name in dir(obj) if name[0] != '_']
We can extend this to provide a copy of the semantic namespace of an object, but we need to exclude __slots__
that aren’t assigned, and if we’re taking the request for “current properties” seriously, we need to exclude calculated properties (as they could become expensive, and could be interpreted as not “current”):
from types import FunctionType
from inspect import getmembers
def attrs(obj):
disallowed_properties = {
name for name, value in getmembers(type(obj))
if isinstance(value, (property, FunctionType))}
return {
name: getattr(obj, name) for name in api(obj)
if name not in disallowed_properties and hasattr(obj, name)}
And now we do not calculate or show the property, quux:
>>> attrs(obj)
{'bar': 0, 'baz': 'baz', 'foo': ''}
Caveats
But perhaps we do know our properties aren’t expensive. We may want to alter the logic to include them as well. And perhaps we want to exclude other custom data descriptors instead.
Then we need to further customize this function. And so it makes sense that we cannot have a built-in function that magically knows exactly what we want and provides it. This is functionality we need to create ourselves.
Conclusion
There is no built-in function that does this, and you should do what is most semantically appropriate for your situation.
In most cases, using __dict__
or dir()
will get you the info you’re wanting. If you should happen to need more details, the standard library includes the inspect module, which allows you to get some impressive amount of detail. Some of the real nuggets of info include:
- names of function and method parameters
- class hierarchies
- source code of the implementation of a functions/class objects
- local variables out of a frame object
If you’re just looking for “what attribute values does my object have?”, then dir()
and __dict__
are probably sufficient. If you’re really looking to dig into the current state of arbitrary objects (keeping in mind that in python almost everything is an object), then inspect
is worthy of consideration.
Hope you learned something from this post.
Follow Programming Articles for more!