597

Is there a way to get all attributes/methods/fields/etc. of an object in Python?

vars() is close to what I want, but it doesn't work unless an object has a __dict__, which isn't always true (e.g. it's not true for a list, a dict, etc.).

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    For getting the attributes of a `attrs` object, there's [`__attrs_attrs__`](https://www.attrs.org/en/stable/extending.html). – user202729 Jan 29 '21 at 09:16

4 Answers4

785

Use the built-in function dir().

mouad
  • 67,571
  • 18
  • 114
  • 106
  • 29
    note that the behavior of `dir()` is often manipulated to show *interesting* attributes, rather than strictly all; for instance it doesn't show attributes inherited through a metaclass, or it may be overridden with a `__dir__` method. – SingleNegationElimination Jul 30 '11 at 23:11
  • 13
    @TokenMacGuy: It's fine for what I need... thinking about it too much would give me a headache. ;) – user541686 Jul 30 '11 at 23:16
  • It also doesn't show introspection properties such as `func_defaults`. – fluffy Aug 18 '15 at 07:24
  • 1
    @mouad why do I get this though >> dir(pyrenderdoc) IronPython.Runtime.List ? – Mona Jalal Dec 30 '16 at 02:21
175

I use __dict__ and dir(<instance>)

Example:

class MyObj(object):
  def __init__(self):
    self.name = 'Chuck Norris'
    self.phone = '+6661'

obj = MyObj()
print(obj.__dict__)
print(dir(obj))

# Output:  
# obj.__dict__ --> {'phone': '+6661', 'name': 'Chuck Norris'}
#
# dir(obj)     --> ['__class__', '__delattr__', '__dict__', '__doc__',
#               '__format__', '__getattribute__', '__hash__', 
#               '__init__', '__module__', '__new__', '__reduce__', 
#               '__reduce_ex__', '__repr__', '__setattr__', 
#               '__sizeof__', '__str__', '__subclasshook__', 
#               '__weakref__', 'name', 'phone']
Slipstream
  • 13,455
  • 3
  • 59
  • 45
  • 21
    The OP *specifically* mentions that he is interested in cases where there is no `__dict__` - otherwise he could use `vars` – strubbly Sep 08 '16 at 14:23
  • 2
    vars() is close to what I want, but it doesn't work unless an object has a __dict__, which isn't always true (e.g. it's not true for a list, a dict, etc.). he clearly said that is NOT what he wants – legend-is-back Aug 07 '17 at 09:58
121

What you probably want is dir().

The catch is that classes are able to override the special __dir__ method, which causes dir() to return whatever the class wants (though they are encouraged to return an accurate list, this is not enforced). Furthermore, some objects may implement dynamic attributes by overriding __getattr__, may be RPC proxy objects, or may be instances of C-extension classes. If your object is one these examples, they may not have a __dict__ or be able to provide a comprehensive list of attributes via __dir__: many of these objects may have so many dynamic attrs it doesn't won't actually know what it has until you try to access it.

In the short run, if dir() isn't sufficient, you could write a function which traverses __dict__ for an object, then __dict__ for all the classes in obj.__class__.__mro__; though this will only work for normal python objects. In the long run, you may have to use duck typing + assumptions - if it looks like a duck, cross your fingers, and hope it has .feathers.

Eli Collins
  • 8,375
  • 2
  • 34
  • 38
91

You can use dir(your_object) to get the attributes and getattr(your_object, your_object_attr) to get the values

usage :

for att in dir(your_object):
    print (att, getattr(your_object,att))
Jason Angel
  • 2,233
  • 1
  • 14
  • 14