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


Groups > comp.lang.python > #28931

Re: generators as decorators simple issue

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: generators as decorators simple issue
Date 2012-09-12 08:08 +0200
Organization A newly installed InterNetNews server
Message-ID <k2p8s7$jlc$1@r03.glglgl.gl> (permalink)
References <e738542a-f864-4ad0-9aaa-738047356841@googlegroups.com>

Show all headers | View raw


Am 12.09.2012 04:28 schrieb j.m.dagenhart@gmail.com:
> I'm trying to call SetName on an object to prevent me from ever having to call it explictly again on that object. Best explained by example.
>
>
> def setname(cls):
>      '''this is the proposed generator to call SetName on the object'''
>      try:
>          cls.SetName(cls.__name__)
>      finally:
>          yield cls
>
>
> class Trial:
>      '''class to demonstrate with'''
>      def SetName(self, name):
>          print 1, 1
>
> @setname
> class Test(Trial):
>      '''i want SetName to be called by using setname as a decorator'''
>      def __init__(self):
>
>          print 'Yay! or Invalid.'
>
> if __name__ == '__main__':
>      test = Test()
>
>
> How can i fix this?

I am not sure what exactly you want to achieve, but I see 2 problems here:

1. Your setname operates on a class, but your SetName() is an instance 
function.

2. I don't really understand the try...finally yield stuff. As others 
already said, you probably just want to return. I don't see what a 
generator would be useful for here...

def setname(cls):
      '''this is the proposed generator to call SetName on the object'''
      try:
          cls.SetName(cls.__name__)
      finally:
          return cls

and

class Trial(object):
     '''class to demonstrate with'''
     @classmethod
     def SetName(cls, name):
         print 1, 1

should solve your problems.

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