Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17041
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <arnodel@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'called.': 0.09; 'chose': 0.09; 'decorator': 0.09; 'descriptor': 0.09; 'first:': 0.09; 'def': 0.13; 'protocol': 0.15; '**kwargs):': 0.16; '*args,': 0.16; 'decorators.': 0.16; 'enlighten': 0.16; 'functools': 0.16; 'happens:': 0.16; 'obj,': 0.16; 'skip:\xc2 40': 0.16; 'str):': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'previously': 0.19; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'header:In- Reply-To:1': 0.22; 'cc:2**0': 0.24; 'consist': 0.24; 'object,': 0.24; 'skip:_ 20': 0.26; "i'm": 0.26; 'function': 0.27; 'import': 0.27; 'compare': 0.28; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.28; 'partial': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'lines': 0.30; 'confused': 0.30; 'object.': 0.30; 'python3': 0.30; 'skip:\xc2 20': 0.30; '(as': 0.31; 'usually': 0.31; 'skip:( 20': 0.31; 'actual': 0.32; 'me?': 0.32; 'received:209.85.216.46': 0.32; 'received:mail- qw0-f46.google.com': 0.32; 'implement': 0.32; 'actually': 0.33; 'there': 0.33; 'object': 0.33; 'someone': 0.34; 'checking': 0.34; 'creates': 0.34; 'something': 0.35; 'however,': 0.36; 'starting': 0.36; '(to': 0.37; 'bound': 0.37; 'class.': 0.37; 'two': 0.37; 'but': 0.37; 'reasons': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'received:209.85': 0.38; 'created': 0.38; 'why': 0.39; 'called': 0.40; 'group,': 0.40; 'received:209': 0.40; '8bit%:8': 0.40; 'within': 0.60; 'type': 0.61; '2011': 0.61; 'here.': 0.66; 'blank': 0.74; 'skip:\xc2 10': 0.74; '__call__': 0.84; 'bar:': 0.84; 'checker': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=lQbSISvqAgFXRIRxUkNkTqZkMEcyqF7L+2ywU/BkLzI=; b=wi36vTRjl5sxWOw3jY8vwpospc5aQT19yUgJNpzjILJi4nCF9AXomInPoe8O54xWbP iwyngvIFTJYLlwu9g0sv4Zf2B2ZLsH5n994bXOXNysKH8eA2Y2/nbc4kK35K3zpBv+BW /8FclBJMqNB27cthI7jsPBLB4FDUJqRkR0ZAs= |
| MIME-Version | 1.0 |
| In-Reply-To | <jc4vg9$8eo$1@speranza.aioe.org> |
| References | <jc4vg9$8eo$1@speranza.aioe.org> |
| Date | Mon, 12 Dec 2011 13:45:20 +0000 |
| Subject | Re: Confusion about decorators |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| To | Henrik Faber <hfaber@invalid.net> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3544.1323697523.27778.python-list@python.org> (permalink) |
| Lines | 71 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323697523 news.xs4all.nl 6979 [2001:888:2000:d::a6]:37649 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17041 |
Show key headers only | View raw
On 12 December 2011 13:27, Henrik Faber <hfaber@invalid.net> wrote:
> Hi group,
>
> I'm a bit confused regarding decorators. Recently started playing with
> them with Python3 and wanted (as an excercise) to implement a simple
> type checker first: I know there are lots of them out there, this is
> actually one of the reasons I chose that particular function (to compare
> my solution against other, proven solutions).
>
> Starting with a blank slate, I did something along the lines of:
>
> class _TypeCheckedFunction():
> def __init__(self, decoratedfunction):
> self._decoratedfunction = decoratedfunction
>
> def __call__(self, *args, **kwargs):
> [...] Actual checking
>
> def typecheck(wrappedfunction):
> checkfunction = _TypeCheckedFunction(wrappedfunction)
> functools.update_wrapper(checkfunction, wrappedfunction)
> return checkfunction
>
> And decorate my methods like
>
> @typecheck
> def setbar(self, bar: str):
>
> This works somewhat. The problem is, however, when the method is
> actually called. This is what happens:
>
> 1. The decorator is called upon import of the decorated class. It
> creates a _TypeCheckedFunction(setbar) object.
> 2. When setbar is actually called (blubb.setbar("fooobar")), the
> __call__ method of the previously created _TypeCheckedFunction is invoked.
> 3. When trying to call self._decoratedfunction from within that object,
> this fails: "self" is missing! self._decoratedfunction is only the
> *function*, not the bound function of the object that contains setbar().
> Therefore I cannot proceed here.
>
> Solutions that I have seen working usually consist of two functions
> wrapped in each other, but I do not know why the additional introduction
> of a class makes everything fail.
>
> Can someone please enlighten me?
You can (need to?) use the descriptor protocol to deal with methods.
from functools import partial
class _TypeCheckedFunction():
def __init__(self, decoratedfunction):
self._decoratedfunction = decoratedfunction
def __call__(self, *args, **kwargs):
[...] Actual checking
def __get__(self, obj, objtype):
return partial(self, obj)
(Untested)
HTH
--
Arnaud
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Confusion about decorators Henrik Faber <hfaber@invalid.net> - 2011-12-12 14:27 +0100
Re: Confusion about decorators Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-12 13:37 +0000
Re: Confusion about decorators Henrik Faber <hfaber@invalid.net> - 2011-12-12 14:45 +0100
Re: Confusion about decorators Arnaud Delobelle <arnodel@gmail.com> - 2011-12-12 13:45 +0000
Re: Confusion about decorators Henrik Faber <hfaber@invalid.net> - 2011-12-12 14:52 +0100
Re: Confusion about decorators Arnaud Delobelle <arnodel@gmail.com> - 2011-12-12 14:01 +0000
Re: Confusion about decorators Henrik Faber <hfaber@invalid.net> - 2011-12-12 16:35 +0100
csiph-web