Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: Can I find the class of a method in a decorator. Date: Sat, 5 Mar 2016 17:21:13 +0100 Lines: 56 Message-ID: References: <56DAF5A7.9080503@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1TYN+dDSzixctwmK2ehh+QnZj4NwwC5oqqGb/Swo408A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'run,': 0.07; 'cmd': 0.09; 'extern': 0.09; 'subject:method': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; '...)': 0.16; '2016': 0.16; 'received:adsl-dyn.isp.belgacom.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; 'work,': 0.21; 'decorator': 0.22; 'suppose': 0.22; 'trying': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'chris': 0.26; 'external': 0.27; 'not.': 0.27; 'function': 0.28; 'idea': 0.28; 'idea?': 0.29; 'raise': 0.29; 'work.': 0.30; 'class.': 0.30; 'received:be': 0.30; 'candidate': 0.31; 'anybody': 0.32; 'class': 0.33; 'problem': 0.33; 'could': 0.35; 'something': 0.35; "isn't": 0.35; 'there': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'wanted': 0.37; 'subject:the': 0.39; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'mar': 0.65; 'obvious': 0.76; 'angelico:': 0.84; 'chrisa': 0.84; 'pardon': 0.84; 'received:195.238': 0.84; 'schreef': 0.84; 'received:192.168.1.7': 0.91; 'subject:find': 0.91; 'try.': 0.91 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2C7AQByBttW/zbe9VENUIR5U6swkDQxhV4CgV4RAQEBAQEBAYVNAQEEeBELGAkWDwkDAgECAUUTBgICt32LS4QDAQEIAgEdhheEPYUGg24FjTCJeoFKjCOOeo5VNoQRaQGJPwEBAQ User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.5.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104103 Op 05-03-16 om 16:18 schreef Chris Angelico: > On Sun, Mar 6, 2016 at 2:05 AM, Antoon Pardon > 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: , 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 > The idea is that some of these methods will be externally available and others are not. So that I get an external string and can do something of the following: tryout = Tryout() st = read_next_cmd() if st in tryout.allowed: getattr(tryout, st)() else: raise ValueError("%s: unallowed cmd string" % st) And the way I wanted to populate Tryout.allowed as a class attribute would have been with the decorator extern, which would just have put the name of the method in the Tryout.allowed set and then return the function.