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


Groups > comp.lang.python > #28972

Re: generators as decorators simple issue

Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'attribute': 0.05; 'method.': 0.05; "'',": 0.07; '__name__': 0.07; 'decorator': 0.07; 'python': 0.09; '(although': 0.09; '__init__': 0.09; 'dict': 0.09; 'name):': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'runtime': 0.09; 'sep': 0.09; 'stored': 0.10; 'def': 0.10; 'applies': 0.15; "'__main__':": 0.16; '*instance*': 0.16; '@classmethod': 0.16; 'attribute,': 0.16; 'dictionary.': 0.16; 'line),': 0.16; 'none"': 0.16; 'subject:issue': 0.16; 'subject:simple': 0.16; 'test()': 0.16; 'later': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'appropriate': 0.20; 'skip:" 30': 0.20; 'decorators': 0.22; 'visible': 0.22; 'demonstrate': 0.23; 'purposes': 0.23; 'sets': 0.23; "haven't": 0.23; 'idea': 0.24; 'pass': 0.25; 'header:In- Reply-To:1': 0.25; 'leave': 0.26; 'am,': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'post': 0.28; 'behaviour': 0.29; 'dictionary': 0.29; 'implied': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'received:209.85.215.46': 0.30; 'error': 0.30; 'print': 0.32; 'certain': 0.33; 'instances': 0.33; 'right?': 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; 'received:google.com': 0.34; 'so,': 0.35; 'received:209.85': 0.35; 'but': 0.36; '12,': 0.36; 'method': 0.36; 'test': 0.36; 'should': 0.36; 'possible': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'remove': 0.61; "you'll": 0.62; "'class'": 0.84; '5:15': 0.84; "class's": 0.84; 'dict,': 0.84; 'perspective,': 0.84; 'to:name:python': 0.84; 'unclear': 0.84; 'shadow': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=WPmzSWrzlORYnyV5oKWWTFtsHCtJC+yRt9d3nz57IWs=; b=nJ2oiSv1S01Q7s7DZwk3dWVi8CzFNm5lXt6qAUyyIWcxnQFQ5ibclnpI63eEInigIF YSXHTgZ1prF6gxEIyOPfJ97+FBNMjM4Npt81rZC40s2dRyItHAjmi1GsdbDmzm/KS/na D/q/tZf6R9sec2pjYfnqj1nSsHNjbnqaEEpewiEzRtpZGmFmKGKIGWi9lTN2ymmabZm2 lZf1q1ye2mneyLAOENUOx3NwGo5MjipQLl0BRBhLwtSypAiuD5E2/Y5idu9RGT1/n+oc xtvjMnKWpEwwM1GyMJqRtDQtQbA3LLOYRtv6oaXuqD/hCAroV6UGsQckHDjaOB3NU5vQ KOoA==
MIME-Version 1.0
In-Reply-To <3ffa457e-7836-46d0-8246-03b6bd90a025@googlegroups.com>
References <e738542a-f864-4ad0-9aaa-738047356841@googlegroups.com> <3ffa457e-7836-46d0-8246-03b6bd90a025@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 12 Sep 2012 09:09:08 -0600
Subject Re: generators as decorators simple issue
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.566.1347462581.27098.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347462581 news.xs4all.nl 6871 [2001:888:2000:d::a6]:44249
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:28972

Show key headers only | View raw


On Wed, Sep 12, 2012 at 4:22 AM, pyjoshsys <j.m.dagenhart@gmail.com> wrote:
> The output is still not what I want. Now runtime error free, however the output is not what I desire.

[SNIP]

> class Trial(object):
>     '''class to demonstrate with'''
>     def __init__(self):
>         object.__init__(self)
>         self.name = None
>
>     @classmethod
>     def SetName(cls, name):
>         cls.name = name

[SNIP]

> if __name__ == '__main__':
>     test = Test()
>     print 'instance'
>     print '', test.name #should be Test
>     print 'class'
>     print '', Test.name
>
>
> The output is: python decors2.py
> instance
>  None
> class
>  Test
>
> I want:
> instance
>  Test
> class
>  Test
>
> Is this possible in this manner?


The SetName class method sets the name on the *class* dictionary.  The
class's __init__ method also sets a name (None) on the *instance*
dictionary.  From an instance's perspective, the instance dictionary
will shadow the class dictionary.  If you remove the attribute from
the instance dictionary entirely (delete the "self.name = None" line),
and leave the class dictionary as is, then you will get the output you
want (although from your later post I am not certain that this is the
behaviour you want).


On Wed, Sep 12, 2012 at 5:15 AM, pyjoshsys <j.m.dagenhart@gmail.com> wrote:
> so decorators only pass the object and not any instance of the object as the implied argument? Is this right?

Right.

> The idea was to use @setname instead of instance.SetName(instance.__name__).

The appropriate place to do this so that it applies to all instances
of the class rather than to the class would be inside the __init__
method.

Also, instances don't have a __name__ attribute, so it's still unclear
to me what you're looking for.  Did you mean the effect to be that of
"instance.SetName(cls.__name__)"?  If so, then the decorator approach
(with the line "self.name = None" removed) should be fine for your
purposes -- you'll just have the name stored in the class dict instead
of in each instance dict, but it will still be visible as long as you
haven't shadowed it.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

generators as decorators simple issue j.m.dagenhart@gmail.com - 2012-09-11 19:28 -0700
  Re: generators as decorators simple issue Ramchandra Apte <maniandram01@gmail.com> - 2012-09-11 19:55 -0700
  Re: generators as decorators simple issue alex23 <wuwei23@gmail.com> - 2012-09-11 21:39 -0700
  Re: generators as decorators simple issue Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-12 08:08 +0200
  Re: generators as decorators simple issue pyjoshsys <j.m.dagenhart@gmail.com> - 2012-09-12 03:22 -0700
    Re: generators as decorators simple issue Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-09-12 11:47 +0100
    Re: generators as decorators simple issue Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-12 09:09 -0600
  Re: generators as decorators simple issue pyjoshsys <j.m.dagenhart@gmail.com> - 2012-09-12 04:15 -0700

csiph-web