Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28972
| References | <e738542a-f864-4ad0-9aaa-738047356841@googlegroups.com> <3ffa457e-7836-46d0-8246-03b6bd90a025@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-09-12 09:09 -0600 |
| Subject | Re: generators as decorators simple issue |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.566.1347462581.27098.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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