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


Groups > comp.lang.python > #104099 > unrolled thread

Re: Can I find the class of a method in a decorator.

Started byChris Angelico <rosuav@gmail.com>
First post2016-03-06 02:18 +1100
Last post2016-03-06 02:18 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Can I find the class of a method in a decorator. Chris Angelico <rosuav@gmail.com> - 2016-03-06 02:18 +1100

#104099 — Re: Can I find the class of a method in a decorator.

FromChris Angelico <rosuav@gmail.com>
Date2016-03-06 02:18 +1100
SubjectRe: Can I find the class of a method in a decorator.
Message-ID<mailman.227.1457191126.20602.python-list@python.org>
On Sun, Mar 6, 2016 at 2:05 AM, Antoon Pardon
<antoon.pardon@rece.vub.ac.be> wrote:
> Using python 3.4/3.5
>
> Suppose I have the following class:
>
> class Tryout:
>
>     @extern
>     def method(self, ...)
>
> Now how can I have access to the Tryout class in
> the extern function when it is called with method
> as argument
>
> def extern(f):
>     the_class = ????
>
> f.__class doesn't work, if I write the following
>
> def extern(f)
>     print(f.__class__)
>
> the result is: <class 'function'>, so that doesn't work.
> Looking around I didn't find an other obvious candidate
> to try. Anybody an idea?

At the time when the function decorator is run, there isn't any class.
You could just as effectively create your function outside the class
and then inject it (Tryout.method = method).

What is it you're trying to do? Would it be a problem to have a class
decorator instead/as well?

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web