3

Pythonのあるクラスのインスタンスが与えられた場合、各メソッドとプロパティを定義したソースコードの行を判別できると便利です(たとえば、 1を実装するため)。たとえば、モジュール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ます。これは、熱心なダイナミズムによる最新の割り当てではなく、クラス本体での定義を意味します。一部の属性に対して「不明」を返す場合は問題ありません。

>>> 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を内省するときにこれを使用したいと思います。ここでは、すべてのオブジェクトに数百のメソッドがあり、アルファベットだけでなくクラス別に整理すると非常に便利です。

もちろん、Pythonでは常に合理的に知ることはできませんが、ほとんど静的なコードの一般的なケースで良い答えを得るのは素晴らしいことです。

4

3 に答える 3

3

文書化されていない関数を探していますinspect.classify_class_attrs(cls)。クラスを渡すと、タプルのリストが返されます('name', 'kind' e.g. 'method' or 'data', defining class, property)。特定のインスタンスの絶対にすべてに関する情報が必要な場合は、追加の作業を行う必要があります。

例:

>>> 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),
 ...
 ]
于 2009-01-27T22:11:53.177 に答える
2

これは、静的分析なしでは多かれ少なかれ不可能であり、それでも常に機能するとは限りません。コードオブジェクトを調べることで、関数が定義された行とそのファイルを取得できますが、それ以上にできることはあまりありません。モジュールはこれinspectに役立ちます。そう:

import ab
a = ab.A()
meth = a.x
# So, now we have the method.
func = meth.im_func
# And the function from the method.
code = func.func_code
# And the code from the function!
print code.co_firstlineno, code.co_filename

# Or:
import inspect
print inspect.getsource(meth), inspect.getfile(meth)

ただし、次のことを考慮してください。

def some_method(self):
    pass
ab.A.some_method = some_method
ab.A.some_class_attribute = None

またはさらに悪い:

some_cls = ab.A
some_string_var = 'another_instance_attribute'
setattr(some_cls, some_string_var, None)

特に後者の場合、何を手に入れたい、または期待していますか?

于 2009-01-27T19:56:49.410 に答える
1

特に、検査モジュールを探しています。例えばinspect.getsourcefile()inspect.getsourcelines()

a.py:

class Hello(object):
    def say(self):
       print 1

>>> from a import Hello
>>> hi = Hello()
>>> inspect.getsourcefile(hi.say)
a.py
>>> inspect.getsourcelines(A, foo)
(['   def say(self):\n        print 1\n'], 2)

Pythonの動的な性質を考えると、より複雑な状況でこれを行うことは、単純に不可能かもしれません...

于 2009-01-27T19:52:26.937 に答える