如何在Python 2.x中对对象进行自省? [英] How Do I Perform Introspection on an Object in Python 2.x?

查看:75
本文介绍了如何在Python 2.x中对对象进行自省?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 2.x,并且有一个要从以太召唤出来的对象;它上的文档不是特别清楚.我希望能够获得该对象的属性列表以及每个属性的类型.

I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property.

同样,我也想获得该对象的方法列表,以及我可以在该方法上找到的任何其他信息,例如参数数量及其各自的类型.

Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types.

我觉得我只是在Google搜索中缺少正确的术语.我并不是想破坏细节,而是Active Directory,所以总是很有趣.

I have a feeling that I am simply missing the correct jargon in my Google searches. Not that I want to derail with specifics, but it's Active Directory, so that's always fun.

推荐答案

嗯...您的第一站将是一个简单的dir(object).这将向您显示对象的所有成员,包括字段和方法.在交互式Python shell中尝试一下,然后玩一些.

Well ... Your first stop will be a simple dir(object). This will show you all the object's members, both fields and methods. Try it in an interactive Python shell, and play around a little.

例如:

> class Foo:
   def __init__(self):
    self.a = "bar"
    self.b = 4711

> a=Foo()
> dir(a)
['__doc__', '__init__', '__module__', 'a', 'b']

这篇关于如何在Python 2.x中对对象进行自省?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆