Re: how to repeat function definitions less

Discussion in 'Python' started by skip@pobox.com, Mar 14, 2009.

  1. Guest

    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)
    ...

    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"):
    self.write(value,color,level)

    That still leaves you with the need to pass in the color though:

    self.red(value, "red")

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

    --
    Skip Montanaro - - http://www.smontanaro.net/
    , Mar 14, 2009
    #1
    1. Advertising

  2. I would suggest using functools.partial like this
    from functools import partial
    class Foo(object):
    #...
    red = partial(color='red')
    Daniel Neuhäuser, Mar 14, 2009
    #2
    1. Advertising

  3. On Mar 15, 12:09 am, 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
    Michele Simionato, Mar 15, 2009
    #3
  4. On Sun, Mar 15, 2009 at 7:33 AM, alex goretoy
    <> wrote:
    > 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


    Well, the typical usage for class decorators is to dynamically add
    methods to a class, as you
    have just seen. I also suggest you to look at this recipe by Raymond
    Hettinger, about adding
    comparison methods: http://code.activestate.com/recipes/576685/
    Another typical use case is to register classes. You can also use
    class decorators to modify
    class (for instance, think of a "debug" class decorator replacing
    methods with a debug-friendly
    version of them).

    Michele Simionato
    Michele Simionato, Mar 15, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis Lee Bieber
    Replies:
    2
    Views:
    272
    Marc 'BlackJack' Rintsch
    Mar 17, 2009
  2. jiajia wu
    Replies:
    0
    Views:
    344
    jiajia wu
    Oct 1, 2009
  3. 6668
    Replies:
    0
    Views:
    140
  4. lllll
    Replies:
    0
    Views:
    120
    lllll
    Jun 8, 2009
  5. knoak

    Repeat function (NEWBIE)

    knoak, Jun 18, 2004, in forum: Javascript
    Replies:
    2
    Views:
    88
    Mick White
    Jun 18, 2004
Loading...

Share This Page