how to repeat function definitions less

skip at skip at
Sun Mar 15 00:09:51 CET 2009

    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'):
    def gold(self,value,color='gold',level='INFO'):

How about using __getattr__?  Something like:

    def __getattr(self, attr):
        if attr in ("red", "gold", ...):
            return self.write_color

    def write_color(self, value, color, level="INFO"):

That still leaves you with the need to pass in the color though:, "red")

which violates the DRY principle.  I'm sure brighter minds than mine will
come up with a better solution.

Skip Montanaro - skip at -

More information about the Python-list mailing list