Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21809
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cosmius@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'instance,': 0.05; 'subject:Python': 0.05; 'none:': 0.07; 'override': 0.07; 'ugly': 0.07; 'python': 0.08; 'assert': 0.09; 'foo': 0.09; 'subject:method': 0.09; 'subject:reference': 0.09; 'to:addr:comp.lang.python': 0.09; 'def': 0.13; "subject:' ": 0.15; 'luna': 0.16; 'name):': 0.16; 'py3k': 0.16; 'self)': 0.16; 'subject: \n ': 0.16; 'subject:which': 0.16; 'cc:addr:python- list': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'defined': 0.24; 'skip:m 30': 0.24; 'cc:2**0': 0.26; 'code': 0.26; 'says': 0.27; "i'm": 0.28; 'pass': 0.29; 'class': 0.29; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; 'but...': 0.30; 'lot.': 0.30; 'porting': 0.30; 'equivalent': 0.31; 'quite': 0.31; 'subject:?': 0.31; 'thanks': 0.32; 'does': 0.32; 'there': 0.33; 'header:User- Agent:1': 0.33; '17,': 0.34; '...': 0.35; 'subject:How': 0.35; 'but': 0.37; 'reference': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'think': 0.38; 'right,': 0.39; 'received:209': 0.39; 'march': 0.61; 'below': 0.62; 'saturday,': 0.64; 'want,': 0.71; "'foo'": 0.84; 'controller,': 0.84; 'otten': 0.84; 'controller': 0.91 |
| Newsgroups | comp.lang.python |
| Date | Sat, 17 Mar 2012 03:04:58 -0700 (PDT) |
| In-Reply-To | <mailman.754.1331976324.3037.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=74.207.251.187; posting-account=oNuSIgoAAAAZId4Ea0hBMGd7JwNhEw7j |
| References | <20411334.2044.1331962234309.JavaMail.geo-discussion-forums@yncd8> <mailman.754.1331976324.3037.python-list@python.org> |
| User-Agent | G2/1.0 |
| X-Google-Web-Client | true |
| MIME-Version | 1.0 |
| Subject | Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? |
| From | Cosmia Luna <cosmius@gmail.com> |
| To | comp.lang.python@googlegroups.com |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Message-ID | <mailman.755.1331978701.3037.python-list@python.org> (permalink) |
| Lines | 86 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1331978701 news.xs4all.nl 6852 [2001:888:2000:d::a6]:37443 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:21809 |
Show key headers only | View raw
On Saturday, March 17, 2012 5:25:06 PM UTC+8, Peter Otten wrote:
> Cosmia Luna wrote:
>
> > 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?
>
> class set_class:
> def __init__(self, method):
> self.method = method
> def __get__(self, instance, class_):
> if instance is None:
> method = self.method
> def f(*args, **kw):
> return method(*args, **kw)
> f.im_class = class_
> f.__name__ = method.__name__
> return f
> return self.method.__get__(instance, class_)
>
> class Foo:
> def __init__(self, name):
> self.name = name
> @set_class
> def bar(self):
> print("%s says hello from bar()" % self)
> def __str__(self):
> return self.name
>
> class Bar(Foo):
> pass
>
> assert Foo.bar.im_class is Foo
> assert Bar.bar.im_class is Bar
>
> Foo("Fred").bar()
> Foo.bar(Foo("Fred"))
>
> Bar("Fred").bar()
> Bar.bar(Bar("Fred"))
>
> The cleaner approach is probably:
>
> Rule('/<action>/', endpoint=(RootController, RootController.otheraction))
> ...
> Controller, method = endpoint
> controller = Controller(Request(environ))
> ...
> method(controller)
That's exactly what I want, and I think you're right, the approach below is cleaner.
Rule('/<action>/', endpoint=(RootController, RootController.otheraction)).
Thanks a lot.
Cosmia
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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