how to repeat function definitions less
alex goretoy
aleksandr.goretoy at gmail.com
Sun Mar 15 02:33:02 EDT 2009
sweet, I've been wondering how those work. I've read some stuff about them
and still doesn't make sense to me. Why would I want to use it....Please
explain, thank you
-Alex Goretoy
http://www.goretoy.com
On Sun, Mar 15, 2009 at 1:05 AM, Michele Simionato <
michele.simionato at gmail.com> wrote:
> On Mar 15, 12:09 am, s... at pobox.com wrote:
> > I'm doing this in my code, how to make it define all this functions
> for me
> > with lambda, I've been up for a while and cant seem to figure it out,
> whats
> > the most efficient way to do it? with lambda? how? thx
> >
> > def red(self,value,color='red',level='INFO'):
> > self.write(value,color,level)
> > def gold(self,value,color='gold',level='INFO'):
> > self.write(value,color,level)
> > ...
>
> The first thing coming to my mind is a class decorator:
>
> def addcolors(*colors):
> def dec(cls):
> for color in colors:
> def col(self, value, color=color, level='INFO'):
> self.write(value, color, level)
> col.__name__ = color
> setattr(cls, color, col)
> return cls
> return dec
>
> @addcolors('red', 'gold')
> class C(object):
> def write(self, value, color, level):
> pass
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090315/a88de696/attachment.html>
More information about the Python-list
mailing list