简体   繁体   English

在Python中列出类属性及其类型

[英]Listing Class Attributes and Their Types in Python

One of myClass attributes is not compatible with QListWidget's drag and drop event. myClass属性之一与QListWidget的拖放事件不兼容。 Getting this AssertionError: 获取此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). 我需要跟踪/确定哪个myClass属性负责AssertionError,然后在将其实例作为listItem数据分配给QListWidget之前将其删除(稍后将listItem拖动和拖放时导致AssertingError)。

There are 100+ attrs in myClass. myClass中有100多个attrs。 And I can't find the way to filter the attributes that are clearly not responsible for AssertionError. 而且我找不到筛选显然不负责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. 理想情况下,我想查看myClass属性的名称及其类型:它是方法,这是一个字符串..这是另一个类的实例,等等。

Consider using inspect.getmembers() : 考虑使用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, eg see: 您还可以传递predicate参数,这将有助于过滤列表,例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM