alphanumeric
alphanumeric

Reputation: 19329

Listing Class Attributes and Their Types in Python

One of myClass attributes is not compatible with QListWidget's drag and drop event. Getting this AssertionError:

assert id(obj) not in self.memo

I need to track down/determinate which of myClass attributes is responsible for AssertionError and then to remove it before its instance is assigned to QListWidget as an listItem data (causing the AssertingError later when listItem is dragAndDropped).

There are 100+ attrs in myClass. And I can't find the way to filter the attributes that are clearly not responsible for AssertionError.

print dir(myClassInstance)

prints only the names of the attributes but not their type.

Same useless info is coming from

attributes = [attr for attr in dir(myClassInstance) if not attr.startswith('__')]

Ideally I would like to see the name of myClass attribute and its type: it is method, and that is a string.. and that is the instance of another class and etc.

Upvotes: 1

Views: 71

Answers (1)

alecxe
alecxe

Reputation: 473833

Consider using inspect.getmembers():

>>> import inspect
>>> from datetime import datetime
>>> now = datetime.now()
>>> inspect.getmembers(now)
[('__add__', <method-wrapper '__add__' of datetime.datetime object at 0x105754ee0>),
 ...
('weekday', <built-in method weekday of datetime.datetime object at 0x105754ee0>), ('year', 2014)]

You can also pass a predicate argument which would help in filtering out the list, e.g. see:

Upvotes: 2

Related Questions