Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to repeat function definitions less

Reply
Thread Tools

Re: how to repeat function definitions less

 
 
skip@pobox.com
Guest
Posts: n/a
 
      03-14-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'):
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.velocityreviews.com/forums/(E-Mail Removed) - http://www.smontanaro.net/
 
Reply With Quote
 
 
 
 
Daniel Neuhäuser
Guest
Posts: n/a
 
      03-14-2009
I would suggest using functools.partial like this
from functools import partial
class Foo(object):
#...
red = partial(color='red')

 
Reply With Quote
 
 
 
 
Michele Simionato
Guest
Posts: n/a
 
      03-15-2009
On Mar 15, 12:09*am, (E-Mail Removed) 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

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      03-15-2009
On Sun, Mar 15, 2009 at 7:33 AM, alex goretoy
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
More Efficiency,More Benefit,Less Risk,Less Work! jiajia wu ASP .Net 0 10-01-2009 01:50 PM
More Efficiency,More Benefit,Less Risk,Less Work! lllll Ruby 0 06-08-2009 02:10 PM
More Efficiency,More Benefit,Less Risk,Less Work! 6668 Ruby 0 05-14-2009 12:33 AM
A request (was: how to repeat function definitions less Dennis Lee Bieber Python 2 03-17-2009 08:09 AM



Advertisments