Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21806
| From | Cosmia Luna <cosmius@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X? |
| Date | 2012-03-17 01:11 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <2929404.2007.1331971877783.JavaMail.geo-discussion-forums@ynbo9> (permalink) |
| References | <20411334.2044.1331962234309.JavaMail.geo-discussion-forums@yncd8> <32504391.2142.1331969697343.JavaMail.geo-discussion-forums@ynil17> |
On Saturday, March 17, 2012 3:34:57 PM UTC+8, Richard Thomas wrote:
> On Saturday, 17 March 2012 05:30:34 UTC, 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
>
> mthd.im_class is the class of mthd.im_self not the class that defined the method.
>
> >
> > So, how can I get a reference to Foo? This is important when writing
> > decorators, the only way I can think out is:
>
> Not sure what sort of decorators you're writing. Examples?
A decorator is not necessary but a similar function accept a method. I don't
like any of existing web frameworks and try to write one via werkzeug from
pocoo.
from myapp.controllers import RootController
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException
from werkzeug.wrappers import Request
url_map = Map([
Rule('/', endpoint=RootController.index),
Rule('/about', endpoint=RootController.about),
Rule('/contact', endpoint=RootController.contact),
Rule('/<action>/', endpoint=RootController.otheraction)
])
def application(environ, start_response): #this is a WSGI 1.0 app
bound_url_map = url_map.bind_to_environ(environ)
try:
endpoint, args = bound_url_map.match()
except HTTPException, e:
return e(environ, start_response)
Controller = endpoint.im_class
controller = Controller(Request(environ))
if hasattr(controller, '__before__'):
controller.__before__()
endpoint(controller)
if hasattr(controller, '__after__'):
controller.__after__()
response = controller.__get_response__():
return response(environ, start_response)
This is a Pylons-style application, but I don't like others in Pylons.
>
> You can achieve this with metaclasses but if you're using classes from someone else's code this doesn't necessarily work. Something in inspect module can probably do the trick, check the docs. Frankly though it sounds messy no matter what. It might be better to find an alternative to knowing the class.
>
> > 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 | 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