generators as decorators simple issue

alex23 wuwei23 at gmail.com
Wed Sep 12 00:39:52 EDT 2012


On Sep 12, 12:28 pm, j.m.dagenh... at gmail.com wrote:
> def setname(cls):
>     '''this is the proposed generator to call SetName on the object'''
>     try:
>         cls.SetName(cls.__name__)
>     finally:
>         yield cls

A generator is (basically) a callable that acts like an iterator.
You'd use a generator if you wanted to loop with for or a list
comprehension across the output of the generator: for foo in
setname(Test)

A decorator is a callable that takes another callable as an argument,
either modifying it or returning a wrapped version of it: Test =
setname(Test)

You don't want to iterate over anything, so you should change `yield`
to `return`.




More information about the Python-list mailing list