can not use __methods__
Steven Bethard
steven.bethard at gmail.com
Wed Jan 26 15:41:20 EST 2005
ajikoe at gmail.com wrote:
> I try to use __methods__ in python 2.4 and 2.2 it always fail.
> Can some one tell me if I want to itterate the methods in a class and
> print it in a string format ( it is possible using __methods__ ).
> Is there any replacement?
py> class C(object):
... a = 1
... b = 2
... def f(self):
... pass
... def g(self):
... pass
...
py> import inspect
py> for attr, value in C.__dict__.iteritems():
... if inspect.isroutine(value):
... print attr, value
...
g <function g at 0x00C10B70>
f <function f at 0x011AC4B0>
or:
py> for attr, value in vars(C).iteritems():
... if inspect.isroutine(value):
... print attr, value
...
g <function g at 0x00C10B70>
f <function f at 0x011AC4B0>
or if you want to include special methods:
py> for attr in dir(C):
... value = getattr(C, attr)
... if inspect.isroutine(value):
... print attr, value
...
__delattr__ <slot wrapper '__delattr__' of 'object' objects>
__getattribute__ <slot wrapper '__getattribute__' of 'object' objects>
__hash__ <slot wrapper '__hash__' of 'object' objects>
__init__ <slot wrapper '__init__' of 'object' objects>
__new__ <built-in method __new__ of type object at 0x1E1AE6E8>
__reduce__ <method '__reduce__' of 'object' objects>
__reduce_ex__ <method '__reduce_ex__' of 'object' objects>
__repr__ <slot wrapper '__repr__' of 'object' objects>
__setattr__ <slot wrapper '__setattr__' of 'object' objects>
__str__ <slot wrapper '__str__' of 'object' objects>
f <unbound method C.f>
g <unbound method C.g>
This is probably useful for introspection at the Python prompt, but if
you're planning on doing this in your code somewhere, you might present
the problem you're trying to solve to the list, because this is likely
not the best way to approach it...
Steve
More information about the Python-list
mailing list