Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17039
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!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!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <andrea.crotti.0@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'called.': 0.09; 'chose': 0.09; 'decorator': 0.09; 'first:': 0.09; 'anyway': 0.09; 'def': 0.13; 'skip:f 30': 0.13; '**kwargs):': 0.16; '*args,': 0.16; 'decorators.': 0.16; 'enlighten': 0.16; 'happens:': 0.16; 'invoked.': 0.16; 'str"': 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; 'url:code': 0.28; 'bit': 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'lines': 0.30; 'confused': 0.30; 'object.': 0.30; 'python3': 0.30; 'received:mail-bw0-f46.google.com': 0.30; '(as': 0.31; 'usually': 0.31; 'skip:( 20': 0.31; 'does': 0.32; 'actual': 0.32; 'me?': 0.32; 'received:209.85.214': 0.32; 'implement': 0.32; 'message- id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'there': 0.33; 'object': 0.33; 'it.': 0.34; '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; 'could': 0.37; 'received:10.0.0': 0.38; 'received:209.85': 0.38; 'created': 0.38; 'should': 0.39; 'why': 0.39; 'called': 0.40; 'group,': 0.40; 'received:209': 0.40; 'within': 0.60; 'type': 0.61; 'here.': 0.66; 'blank': 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=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=tZck/6IK9mjD9+cma06SSjf1bA6n+N+n+IKMkbSjf1M=; b=eDjDrl4ZrSjasR82AheLKfhNh+Ub4LiNiY4V1bIEbfKdL6CIa05CwQAwCq4iB1sUEL 3hmWHNdFmGcQyEG5ubYbWmlU4D56KNWMeEm8gehoXq5lVBqWGHcHJdq3/tPoR8Na82ze iOFDqzmcVT6FzLexNACTzTOWTWerOcdSAFr8k= |
| Date | Mon, 12 Dec 2011 13:37:32 +0000 |
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111108 Thunderbird/8.0 |
| MIME-Version | 1.0 |
| To | Henrik Faber <hfaber@invalid.net> |
| Subject | Re: Confusion about decorators |
| References | <jc4vg9$8eo$1@speranza.aioe.org> |
| In-Reply-To | <jc4vg9$8eo$1@speranza.aioe.org> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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.3543.1323697057.27778.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323697057 news.xs4all.nl 6968 [2001:888:2000:d::a6]:35470 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17039 |
Show key headers only | View raw
On 12/12/2011 01:27 PM, Henrik Faber 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?
>
> Best regards,
> Henrik
Not sure how that could work in general, what does "bar: str" should do?
Is that a dictionary?
Anyway there is already an implementation if you're interested for type
checking:
http://oakwinter.com/code/typecheck/
You can have a look at how they do it.
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