can not use __methods__

A

ajikoe

Hello,

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?

Sincerely Yours,
Pujo
 
S

Steven Bethard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top