introspection: How to find out the class defining a method

Discussion in 'Python' started by Holger Joukl, May 6, 2004.

  1. Holger Joukl

    Holger Joukl Guest

    Hi,
    introspection works different in python 2.3:

    Python 2.3.3 (#12, Feb 19 2004, 11:42:09)
    [GCC 2.95.2 19991024 (release)] on sunos5
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Foo:

    .... def do(self):
    .... pass
    ....
    >>> class Moo(Foo):

    .... pass
    ....
    >>> x=Moo()
    >>> x.do.im_class

    <class __main__.Moo at 0x1a9030>
    >>>


    whereas:

    Python 2.1 (#3, Jun 1 2001, 15:51:25)
    [GCC 2.95.2 19991024 (release)] on sunos5
    Type "copyright", "credits" or "license" for more information.
    >>> class Foo:

    .... def do(self):
    .... pass
    ....
    >>> class Moo(Foo):

    .... pass
    ....
    >>> x=Moo()
    >>> x.do.im_class

    <class __main__.Foo at cd7b4>
    >>>


    Is there an elegant way to achieve the latter result (i.e. the base class
    in which
    this method actually is defined) in python 2.3, too? Couldn´t find it in
    the docs.

    Cheers
    Holger

    Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
    Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
    verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
    sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
    gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
    garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
    den Inhalt der E-Mail als Hardcopy an.

    The contents of this e-mail are confidential. If you are not the named
    addressee or if this transmission has been addressed to you in error,
    please notify the sender immediately and then delete this e-mail. Any
    unauthorized copying and transmission is forbidden. E-Mail transmission
    cannot be guaranteed to be secure. If verification is required, please
    request a hard copy version.
    Holger Joukl, May 6, 2004
    #1
    1. Advertising

  2. Holger Joukl wrote:
    > Python 2.1 (#3, Jun 1 2001, 15:51:25)
    > [GCC 2.95.2 19991024 (release)] on sunos5
    > Type "copyright", "credits" or "license" for more information.
    >>>> class Foo:

    > ... def do(self):
    > ... pass
    > ...
    >>>> class Moo(Foo):

    > ... pass
    > ...
    >>>> x=Moo()
    >>>> x.do.im_class

    ><class __main__.Foo at cd7b4>
    >>>>

    >
    > Is there an elegant way to achieve the latter result (i.e. the base
    > class in which this method actually is defined) in python 2.3, too?
    > Couldn´t find it in the docs.


    I'm not sure this qualifies as "elegant". I think it gives the right
    answer when it works, though.

    def class_defining_method(method):
    for c in inspect.getmro(method.im_class):
    if c.__dict__.has_key(method.im_func.__name__):
    return c

    Jeremy.
    Jeremy Yallop, May 7, 2004
    #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. johny smith
    Replies:
    8
    Views:
    392
    Peter Koch Larsen
    Jul 2, 2004
  2. Thomas
    Replies:
    4
    Views:
    158
    Thomas
    Jun 7, 2005
  3. Rolf Gebauer
    Replies:
    3
    Views:
    158
    Pit Capitain
    Jul 19, 2005
  4. David Tran
    Replies:
    2
    Views:
    70
    David Tran
    Sep 15, 2005
  5. Glenn
    Replies:
    4
    Views:
    124
    Brian Candler
    Jan 28, 2009
Loading...

Share This Page