Reputation: 2785
I want to find a generic way to list a python object attributes (names and values) but in a concise way.
This works pretty well when using vars()
, but you can't use it if there's no __dict__
; and dir()
is full of uninteresting attributes.
I know that's kind of random, but PyCharm is doing something similar somehow, here's an example for the numpy ndarray
object, which has no __dict__
; and dir()
contains 161 attributes. I tried to remove private and callable values from dir()
, but still, there are dozens.
Any idea for how PyCharm is doing it? Is there a chance they have a special treatment for numpy
objects for example?
Upvotes: 3
Views: 100
Reputation: 39798
For classes defined in Python, you can use __dict__
(if any) on the object plus __slots__
on its type (if any). For built-in types in CPython, you might also want to search the class’s dictionary for objects of type types.GetSetDescriptorType
, types.MemberDescriptorType
, and/or types.DynamicClassAttribute
. This misses most things that have __names__
, though it can still be useful to filter the ones that remain.
Upvotes: 1