Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21801

How to get a reference of the 'owner' class to which a method belongs in Python 3.X?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Cosmia Luna <cosmius@gmail.com>
Newsgroups comp.lang.python
Subject How to get a reference of the 'owner' class to which a method belongs in Python 3.X?
Date Fri, 16 Mar 2012 22:30:34 -0700 (PDT)
Organization http://groups.google.com
Lines 36
Message-ID <20411334.2044.1331962234309.JavaMail.geo-discussion-forums@yncd8> (permalink)
NNTP-Posting-Host 74.207.251.187
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1331962335 9601 127.0.0.1 (17 Mar 2012 05:32:15 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sat, 17 Mar 2012 05:32:15 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=74.207.251.187; posting-account=oNuSIgoAAAAZId4Ea0hBMGd7JwNhEw7j
User-Agent G2/1.0
X-Received-Bytes 1781
Xref csiph.com comp.lang.python:21801

Show key headers only | View raw


I'm porting my existing work to Python 3.X, but...

class Foo:
    def bar(self):
        pass

mthd = Foo.bar

assert mthd.im_class is Foo # this does not work in py3k

So, how can I get a reference to Foo? This is important when writing
decorators, the only way I can think out is:

class Foo:
    def bar(self):
        'Foo' # manually declare the owner class
        pass

mthd = Foo.bar

assert mthd.__globals__[mthd.__doc__] is Foo # this works

class Child(Foo):
    def bar(self):
        'Child' # I have to override all method defined by bar but do nothing
        pass

child_mthd = Child.bar

assert child_mthd.__globals__[child_mthd.__doc__] is Child # this works

But the code above is quite ugly and abuses the __doc__. Is there any
equivalent in py3k of im_class?

Thanks,
Cosmia

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-16 22:30 -0700
  Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Chris Rebert <clp2@rebertia.com> - 2012-03-16 22:51 -0700
  Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Richard Thomas <chardster@gmail.com> - 2012-03-17 00:34 -0700
    Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-17 01:11 -0700
  Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Peter Otten <__peter__@web.de> - 2012-03-17 10:25 +0100
    Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-17 03:04 -0700
      Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-17 05:21 -0700
        Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-18 02:42 -0700
        Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-18 02:42 -0700
          Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-18 10:14 -0600
      Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-17 05:21 -0700
    Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Cosmia Luna <cosmius@gmail.com> - 2012-03-17 03:04 -0700
  Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-17 10:01 +0000

csiph-web