您将如何确定Python类的每个属性和方法的定义位置? [英] How would you determine where each property and method of a Python class is defined?

查看:244
本文介绍了您将如何确定Python类的每个属性和方法的定义位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出Python中某个类的实例,能够确定每种方法和属性定义的源代码行(例如实现1 ).例如,给定一个模块ab.py

Given an instance of some class in Python, it would be useful to be able to determine which line of source code defined each method and property (e.g. to implement 1). For example, given a module ab.py

class A(object):
    z = 1
    q = 2
    def y(self): pass
    def x(self): pass

class B(A):
    q = 4
    def x(self): pass
    def w(self): pass

定义一个函数whither(class_,attribute),该函数返回一个元组,该元组包含源代码中定义或子类化attribute的文件名,类和行.这意味着班级中的定义,而不是由于过度的动态性而导致的最新任务.如果某些属性返回未知",就可以了.

define a function whither(class_, attribute) returning a tuple containing the filename, class, and line in the source code that defined or subclassed attribute. This means the definition in the class body, not the latest assignment due to overeager dynamism. It's fine if it returns 'unknown' for some attributes.

>>> a = A()
>>> b = B()
>>> b.spigot = 'brass'
>>> whither(a, 'z')
("ab.py", <class 'a.A'>, [line] 2)
>>> whither(b,  'q')
("ab.py", <class 'a.B'>, 8)
>>> whither(b, 'x')
("ab.py", <class 'a.B'>, 9)
>>> whither(b, 'spigot')
("Attribute 'spigot' is a data attribute")

我想在对Plone进行内省时使用它,其中每个对象都有数百种方法,对按类而不是仅按字母顺序组织的方法进行排序将非常有用.

I want to use this while introspecting Plone, where every object has hundreds of methods and it would be really useful to sort through them organized by class and not just alphabetically.

当然,在Python中您可能无法始终合理地知道,但是在大多数静态代码的常见情况下,获得良好的答案将是很好的.

Of course, in Python you can't always reasonably know, but it would be nice to get good answers in the common case of mostly-static code.

推荐答案

您正在寻找未记录的函数inspect.classify_class_attrs(cls).将其传递给一个类,它将返回一个元组('name', 'kind' e.g. 'method' or 'data', defining class, property)的列表.如果您需要有关特定实例中绝对所有内容的信息,则必须做其他工作.

You are looking for the undocumented function inspect.classify_class_attrs(cls). Pass it a class and it will return a list of tuples ('name', 'kind' e.g. 'method' or 'data', defining class, property). If you need information on absolutely everything in a specific instance you'll have to do additional work.

示例:

>>> import inspect
>>> import pprint
>>> import calendar
>>> 
>>> hc = calendar.HTMLCalendar()
>>> hc.__class__.pathos = None
>>> calendar.Calendar.phobos = None
>>> pprint.pprint(inspect.classify_class_attrs(hc.__class__))
[...
 ('__doc__',
  'data',
  <class 'calendar.HTMLCalendar'>,
  '\n    This calendar returns complete HTML pages.\n    '),
 ...
 ('__new__',
  'data',
  <type 'object'>,
  <built-in method __new__ of type object at 0x814fac0>),
 ...
 ('cssclasses',
  'data',
  <class 'calendar.HTMLCalendar'>,
  ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']),
 ('firstweekday',
  'property',
  <class 'calendar.Calendar'>,
  <property object at 0x98b8c34>),
 ('formatday',
  'method',
  <class 'calendar.HTMLCalendar'>,
  <function formatday at 0x98b7bc4>),
 ...
 ('pathos', 'data', <class 'calendar.HTMLCalendar'>, None),
 ('phobos', 'data', <class 'calendar.Calendar'>, None),
 ...
 ]

这篇关于您将如何确定Python类的每个属性和方法的定义位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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