can not use __methods__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajikoe@gmail.com

    can not use __methods__

    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

  • Steven Bethard

    #2
    Re: can not use __methods__

    ajikoe@gmail.co m wrote:[color=blue]
    > 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?[/color]

    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__.iter items():
    .... if inspect.isrouti ne(value):
    .... print attr, value
    ....
    g <function g at 0x00C10B70>
    f <function f at 0x011AC4B0>

    or:

    py> for attr, value in vars(C).iterite ms():
    .... if inspect.isrouti ne(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.isrouti ne(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

    Comment

    Working...