Dir () without built-in methods

Is there a way to get all the attributes of an object without inline attachments? I hope to achieve this without a typeset or manual control of double underscores, if possible.

I tried dir, but it gives me all the built-in materials. Ideally, I would like something like

class A():
    foo = 'bar'
>>>> dir(a)
['foo']

instead

>>>> dir(a)
['__doc__', '__module__', 'foo']
+4
source share
1 answer

Do you just want to filter out the “special” methods or do you really know which methods are implemented in the instance itself, are not inherited from the database (or both, since these are different questions)?

You can filter out special methods with something simple enough:

def vdir(obj):
    return [x for x in dir(obj) if not x.startswith('__')]

>>> vdir(a)
['foo']
+5
source

Source: https://habr.com/ru/post/1525153/


All Articles