how to repeat function definitions less
andrew cooke
andrew at acooke.org
Sat Mar 14 19:33:00 EDT 2009
is this what you want (python 3.0)?
>>> class Colours:
... def __init__(self):
... for colour in ['red', 'blue']:
... setattr(self, colour, lambda value, c=colour: self.write(value, c))
... def write(self, value, colour):
... print(value, colour)
...
>>> c = Colours()
>>> c.red(1)
1 red
andrew
alex goretoy 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)
> def green(self,value,color='green',level='INFO'):
> self.write(value,color,level)
> def blue(self,value,color='blue',level='INFO'):
> self.write(value,color,level)
> def yellow(self,value,color='yellow',level='INFO'):
> self.write(value,color,level)
> def purple(self,value,color='purple',level='INFO'):
> self.write(value,color,level)
> def w_red(self,value,color='white_on_red',level='INFO'):
> self.write(value,color,level)
> def w_gold(self,value,color='white_on_gold',level='INFO'):
> self.write(value,color,level)
> def w(self,value,color="red",level="INFO"):
> self.write(value,color,level)
> def write(self,value,color="red",level="INFO"):
> """
> write - output message,take value string or list,color=""
> """
> self._hero(value,color,level)
>
>
> -Alex Goretoy
> http://www.goretoy.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list