can not use __methods__

Discussion in 'Python' started by ajikoe@gmail.com, Jan 26, 2005.

  1. Guest

    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
     
    , Jan 26, 2005
    #1
    1. Advertising

  2. 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
     
    Steven Bethard, Jan 26, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M K
    Replies:
    11
    Views:
    7,104
    woods_jemmy
    Apr 9, 2008
  2. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    1
    Views:
    3,791
    Ken Cox [Microsoft MVP]
    Apr 19, 2005
  3. Chris S
    Replies:
    0
    Views:
    469
    Chris S
    Dec 13, 2006
  4. Ting Wang
    Replies:
    5
    Views:
    197
    John Bokma
    Oct 6, 2005
  5. robertospara
    Replies:
    14
    Views:
    289
    robertospara
    Dec 10, 2006
Loading...

Share This Page