1

Python's dir() is nice, don't get me wrong. But I'd like a list that actually tells me what kind of things the objects are: methods, variables, things that are inherited, etc.

As best I can tell, dir always returns a simple list of strings with no indication as to what the objects are. I checked the documentation for dir() and I don't see any way of getting better information.

Are there any other packages or tools for doing this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Burnt Umber
  • 133
  • 1
  • 9
  • 2
    You're probably looking for [inspect.getmembers](https://docs.python.org/3/library/inspect.html?highlight=inspect#inspect.getmembers) and/or all the other methods that ``inspect`` provides. – Mike Scotty Sep 21 '21 at 12:41
  • These are both great answers. I feel like I should've known about inspect.getmembers. This is a really hard thing to search for though because of the ubiquity of "dir" as a term – Burnt Umber Sep 21 '21 at 16:50

1 Answers1

0

I'd like a list that actually tells me what kind of things the objects are: methods, variables, things that are inherited, etc.

pyclbr built-in module might be useful for you, if you are interested in classes in certain *.py file, let somefile.py content be

class MyClass():
    def parent_method(self):
        return None

class MyChildClass(MyClass):
    def child_method(self):
        return None    

then

import pyclbr
objects = pyclbr.readmodule("somefile")
print(objects['MyChildClass'].super)
print(objects['MyChildClass'].methods)

output

['MyClass']
{'child_method': 6}

Explanation: pyclbr does not execute code, but extract information from python source code. In above example from .super we can conclude that MyChildClass is child of MyClass and that MyChildClass define method child_method in 6th line of line

Daweo
  • 31,313
  • 3
  • 12
  • 25