On 2011-12-08 08:59:26 +0000, Thomas Rachel said:

Am 08.12.2011 08:18 schrieb 88888 Dihedral:
I use the @ decorator to behave exactly like a c macro that
does have fewer side effects.

I am wondering is there other interesting methods to do the
jobs in Python?

In combination with a generator, you can do many funny things.


For example, you can build up a string:

def mkstring(f):
     """Turns a string generator into a string,
     joining with ", ".
     """
     return ", ".join(f())

def create_answer():
     @mkstring
     def people():
         yield "Anna"
         yield "John"
         yield "Theo"

     return "The following people were here: " + people


Many other things are thinkable...


Thomas

I am still perplexed about decorators though, am happily using Python for many years without them, but maybe i am missing something? For example in the above case, if I want the names attached to each other with a comma, why wouldn't I just create a function doing exactly this? Why would I first write a single name generator and then decorate it so that I never can get single names anymore (this is the case, isn't it? Once decorated, I can not get the original behaviour of the function anymore.
So, above, why not
def mkstring(mylist):
with the same function declaration and then just call it with a list of names that I generate elsewhere in my program? I just can't identify the use-case for decorators, but as I said, maybe I am missing something.

Michael


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to