what is lambda used for in real code?

2004-12-30 Thread Steven Bethard
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def st

Re: what is lambda used for in real code?

2004-12-30 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: > (2) lambda a: a.lower() > My first thought here was to use str.lower instead of the lambda, but of > course that doesn't work if 'a' is a unicode object: Right, but string.lower works (after an 'import string'). More generally, maybe it would be nice t

Re: what is lambda used for in real code?

2004-12-31 Thread Diez B. Roggisch
> So, those are my thoughts on how lambdas are "really" used. If others > out there have real-life code that uses lambdas in interesting ways, > feel free to share them here! I use them in conjunction with metaclasses and properties: def _s_item(self, item): """ saw::active """

Re: what is lambda used for in real code?

2004-12-31 Thread Steven Bethard
Alex Martelli wrote: Steven Bethard <[EMAIL PROTECTED]> wrote: (2) lambda a: a.lower() My first thought here was to use str.lower instead of the lambda, but of course that doesn't work if 'a' is a unicode object: Right, but string.lower works (after an 'import string'). More generally, maybe it w

Re: what is lambda used for in real code?

2004-12-31 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: > >>(3) self.plural = lambda n: int(n != 1) > >>Note that this is *almost* writable with def syntax. If only we could do: > >> def self.plural(n): > >> int(n != 1) > > > > Not sure about the context, but maybe we could use, at class-level: >

Re: what is lambda used for in real code?

2004-12-31 Thread Steven Bethard
Alex Martelli wrote: Steven Bethard <[EMAIL PROTECTED]> wrote: py> class C(object): ... def __init__(self): ... self.plural = lambda n: int(n != 1) ... py> c = C() py> c.__class__.plural(1) Traceback (most recent call last): File "", line 1, in ? AttributeError: type object 'C' has no

Re: what is lambda used for in real code?

2004-12-31 Thread Adam DePrince
On Fri, 2004-12-31 at 01:53, Steven Bethard wrote: > I thought it might be useful to put the recent lambda threads into > perspective a bit. I was wondering what lambda gets used for in "real" > code, so I grepped my Python Lib directory. Here are some of the ones I > looked, classified by how

Re: what is lambda used for in real code?

2004-12-31 Thread Steven Bethard
Adam DePrince wrote: Lets not forget the "real reason" for lambda ... the elegance of orthogonality. Why treat functions differently than any other object? We can operate on every other class without having to involve the namespace, why should functions be any different? Yup. I think in most o

Re: what is lambda used for in real code?

2004-12-31 Thread Hans Nowak
Adam DePrince wrote: In sort, we must preserve the ability to create an anonymous function simply because we can do so for every other object type, and functions are not special enough to permit this special case. Your reasoning makes sense... lambda enables you to create a function as part of an

Re: what is lambda used for in real code?

2004-12-31 Thread Steven Bethard
Hans Nowak wrote: Adam DePrince wrote: In sort, we must preserve the ability to create an anonymous function simply because we can do so for every other object type, and functions are not special enough to permit this special case. Your reasoning makes sense... lambda enables you to create a funct

Re: what is lambda used for in real code?

2004-12-31 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: ... > > Your reasoning makes sense... lambda enables you to create a function as > > part of an expression, just like other types can be part of an > > expression. However, by that same reasoning, maybe classes aren't > > special enough either to warr

Re: what is lambda used for in real code?

2004-12-31 Thread Reinhold Birkenfeld
Adam DePrince wrote: >> So, those are my thoughts on how lambdas are "really" used. If others >> out there have real-life code that uses lambdas in interesting ways, >> feel free to share them here! >> > > Lets not forget the "real reason" for lambda ... I really hoped you would point out th

Re: what is lambda used for in real code?

2004-12-31 Thread Terry Reedy
"Adam DePrince" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > In sort, we must preserve the ability to create an anonymous function > simply because we can do so for every other object type, and functions > are not special enough to permit this special case. Please show me how to

Re: what is lambda used for in real code?

2004-12-31 Thread Jp Calderone
On Fri, 31 Dec 2004 22:09:49 -0500, Terry Reedy <[EMAIL PROTECTED]> wrote: > > "Adam DePrince" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > In sort, we must preserve the ability to create an anonymous function > > simply because we can do so for every other object type, and fu

Re: what is lambda used for in real code?

2004-12-31 Thread Aahz
In article <[EMAIL PROTECTED]>, Adam DePrince <[EMAIL PROTECTED]> wrote: > >Unless internationalization was a concern, few people would write: > >THE_ANSWER_IS = "The answer is" >print THE_ANSWER_IS, x However, there's a moderately large (and growing!) set of people who would argue that I18N is *

Re: what is lambda used for in real code?

2005-01-01 Thread Alex Martelli
Terry Reedy <[EMAIL PROTECTED]> wrote: > "Adam DePrince" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > In sort, we must preserve the ability to create an anonymous function > > simply because we can do so for every other object type, and functions > > are not special enough to

Re: what is lambda used for in real code?

2005-01-01 Thread Duncan Booth
Diez B. Roggisch wrote: > The doc string of _s_item contains a token the metaclass is aware of and > creates a wrapper around _s_item. That works nice on methods, but I found > that properties got bound to their functions _before_ the metaclass kicks > in, so the property wasn't called in the wrap

Re: what is lambda used for in real code?

2005-01-01 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Adam DePrince <[EMAIL PROTECTED]> wrote: > We can operate on every other class without having to involve the > namespace, why should functions be any different? def is a weird beast. It does more than just bind a lambda to a name, it also alters the function so

Re: what is lambda used for in real code?

2005-01-01 Thread Diez B. Roggisch
> Why not improve your metaclass wrapping so it knows about properties and > replaces them with properties containing wrapped functions? Erg - never thought of that, actually - it was so fast to introduce the lambda... But after some tinkering with metaclasses, I think it can be done - I have to

Re: what is lambda used for in real code?

2005-01-01 Thread Steve Holden
Adam DePrince wrote: [...] In sort, we must preserve the ability to create an anonymous function simply because we can do so for every other object type, and functions are not special enough to permit this special case. And you'd create an anonymous type how, exactly? regards Steve -- Steve Holden

Re: what is lambda used for in real code?

2005-01-01 Thread Alex Martelli
Steve Holden <[EMAIL PROTECTED]> wrote: > Adam DePrince wrote: > [...] > > > > In sort, we must preserve the ability to create an anonymous function > > simply because we can do so for every other object type, and functions > > are not special enough to permit this special case. > > > And you'd

Re: what is lambda used for in real code?

2005-01-01 Thread Steve Holden
Alex Martelli wrote: Steve Holden <[EMAIL PROTECTED]> wrote: Adam DePrince wrote: [...] In sort, we must preserve the ability to create an anonymous function simply because we can do so for every other object type, and functions are not special enough to permit this special case. And you'd create

Re: what is lambda used for in real code?

2005-01-01 Thread Alex Martelli
Steve Holden <[EMAIL PROTECTED]> wrote: ... > >>And you'd create an anonymous type how, exactly? > > > type('',(),{}) ... > Indeed. And then you'd insert all the methods as lambdas by > > We both know that the Python language framework has enough introspection > capabilities to do

Re: what is lambda used for in real code?

2005-01-03 Thread Jeff Shannon
Steven Bethard wrote: The only ones that make me a little nervous are examples like: inspect.py: def formatargspec(args, varargs=None, varkw=None, ... formatvarargs=lambda name: '*' + name, formatvarkw=lambda name: '**' + name,

Re: what is lambda used for in real code?

2005-01-04 Thread Adam DePrince
On Fri, 2004-12-31 at 17:36, Steven Bethard wrote: > Adam DePrince wrote: > > Lets not forget the "real reason" for lambda ... the elegance of > > orthogonality. Why treat functions differently than any other object? > > > > We can operate on every other class without having to involve the > >

Re: what is lambda used for in real code?

2005-01-04 Thread Adam DePrince
On Fri, 2004-12-31 at 22:09, Terry Reedy wrote: > "Adam DePrince" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > In sort, we must preserve the ability to create an anonymous function > > simply because we can do so for every other object type, and functions > > are not special e

Re: what is lambda used for in real code?

2005-01-04 Thread Adam DePrince
On Sat, 2005-01-01 at 11:42, Steve Holden wrote: > Adam DePrince wrote: > [...] > > > > In sort, we must preserve the ability to create an anonymous function > > simply because we can do so for every other object type, and functions > > are not special enough to permit this special case. > > > An

Re: what is lambda used for in real code?

2005-01-06 Thread Steven Bethard
I wrote: * Functions I don't know how to rewrite Some functions I looked at, I couldn't figure out a way to rewrite them without introducing a new name or adding new statements. [snip] inspect.py: def formatargspec(args, varargs=None, varkw=None, ... formatvara

Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2004-12-31 Thread Robert Brewer
Steven Bethard wrote: > * Rewritable with existing functions > Mainly these are examples of code that can benefit from using the > functions available in the operator module, especially > operator.itemgetter and operator.attrgetter (available in 2.4) > ... > * Rewritable with list comprehensions/g

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-03 Thread Roman Suzi
Hi all, BTW, Alex Martelli and me have created a PEP 312 some time ago (when the debate of inline if was hot). I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for lambdas _with_ arguments, we could de

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-03 Thread Dave Benjamin
Roman Suzi wrote: I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for lambdas _with_ arguments, we could develop PEP 312 further. Well, my vote is still with Ruby-style codeblock syntax, but as a compro

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-03 Thread Steven Bethard
Roman Suzi wrote: I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for lambdas _with_ arguments, we could develop PEP 312 further. Some suggestions from recent lambda threads (I only considered the ones

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-04 Thread Bengt Richter
On Mon, 03 Jan 2005 18:54:06 GMT, Steven Bethard <[EMAIL PROTECTED]> wrote: >Roman Suzi wrote: >> I wish lambdas will not be deprecated in Python but the key to that is >> dropping the keyword (lambda). If anybody could think of a better syntax for >> lambdas _with_ arguments, we could develop PEP

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-04 Thread Roman Suzi
On Mon, 3 Jan 2005, Steven Bethard wrote: > Roman Suzi wrote: > > I wish lambdas will not be deprecated in Python but the key to that is > > dropping the keyword (lambda). If anybody could think of a better syntax for > > lambdas _with_ arguments, we could develop PEP 312 further. > > Some suggest

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-04 Thread Steven Bethard
Bengt Richter wrote: On Mon, 03 Jan 2005 18:54:06 GMT, Steven Bethard <[EMAIL PROTECTED]> wrote: Roman Suzi wrote: I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for lambdas _with_ arguments, we could

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-04 Thread Steven Bethard
Roman Suzi wrote: On Mon, 3 Jan 2005, Steven Bethard wrote: Roman Suzi wrote: I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for lambdas _with_ arguments, we could develop PEP 312 further. Some suggest

Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?)

2005-01-04 Thread Roman Suzi
On Tue, 4 Jan 2005, Steven Bethard wrote: >Roman Suzi wrote: >> On Mon, 3 Jan 2005, Steven Bethard wrote: >> >> >>>Roman Suzi wrote: >>> I wish lambdas will not be deprecated in Python but the key to that is dropping the keyword (lambda). If anybody could think of a better syntax for l

args (was Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?))

2005-01-04 Thread Michael Spencer
Roman Suzi wrote: Maybe this is too outlandish, but I see lambdas as a "quote" mechanism, which presents a possibility to postpone (precisely control, delegate) evaluation. That is, an ovehead for lambda must be much lower but at the same time visible to the programmer: d = a + (lambda x, y: x+ y)

Re: args (was Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?))

2005-01-04 Thread Roman Suzi
On Tue, 4 Jan 2005, Michael Spencer wrote: >Roman Suzi wrote: > >> Maybe this is too outlandish, but I see lambdas as a "quote" mechanism, >> which presents a possibility to postpone (precisely control, delegate) >> evaluation. That is, an ovehead for lambda must be much lower but at the >> same t

Re: args (was Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?))

2005-01-07 Thread Bengt Richter
On Tue, 04 Jan 2005 14:11:51 -0800, Michael Spencer <[EMAIL PROTECTED]> wrote: [Hm, this didn't go out for some reason. I'll just send it now.] >Roman Suzi wrote: > >> Maybe this is too outlandish, but I see lambdas as a "quote" mechanism, >> which presents a possibility to postpone (precisely con