Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Lambda going out of fashion

Reply
Thread Tools

Lambda going out of fashion

 
 
Stephen Thorne
Guest
Posts: n/a
 
      12-23-2004
Hi guys,

I'm a little worried about the expected disappearance of lambda in
python3000. I've had my brain badly broken by functional programming
in the past, and I would hate to see things suddenly become harder
than they need to be.

An example of what I mean is a quick script I wrote for doing certain
actions based on a regexp, which I will simlify in this instance to
make the pertanant points more relevent.

{
'one': lambda x.blat(),
'two': lambda x.blah(),
}.get(someValue, lambda x:0)(someOtherValue)

The alternatives to this, reletively simple pattern, which is a rough
parallel to the 'switch' statement in C, involve creating named
functions, and remove the code from the context it is to be called
from (my major gripe).

So, the questions I am asking are:
Is this okay with everyone?
Does anyone else feel that lambda is useful in this kind of context?
Are there alternatives I have not considered?

merrily-yr's
Stephen.
 
Reply With Quote
 
 
 
 
Keith Dart
Guest
Posts: n/a
 
      12-23-2004
On 2004-12-23, Stephen Thorne <> wrote:
> Hi guys,
>
> I'm a little worried about the expected disappearance of lambda in
> python3000. I've had my brain badly broken by functional programming
> in the past, and I would hate to see things suddenly become harder
> than they need to be.


I use Python lambda quite a bit, and I don't understand the recent noise
about problems with it, and its removal. I don't have a problem with
lambdas.

My personal gripe is this. I think the core language, as of 2.3 or 2.4
is very good, has more features than most people will ever use, and they
(Guido, et al.) can stop tinkering with it now and concentrate more on
the standard libraries.


--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
Keith Dart <>
public key: ID: F3D288E4
================================================== ===================
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-23-2004
Stephen Thorne wrote:
>
> {
> 'one': lambda x.blat(),
> 'two': lambda x.blah(),
> }.get(someValue, lambda x:0)(someOtherValue)
>
> The alternatives to this, reletively simple pattern, which is a rough
> parallel to the 'switch' statement in C, involve creating named
> functions, and remove the code from the context it is to be called
> from (my major gripe).


Here's what my code for a very similar situation usually looks like:

py> class Foo(object):
.... def blat(self):
.... print "blat"
.... def blah(self):
.... print "blah"
....
py> key, foo = 'two', Foo()
py> try:
.... result = dict(one=Foo.blat, two=Foo.blah)[key](foo)
.... except KeyError:
.... result = 0
....
blah

As you can see, I just use the unbound methods of the parent class
directly. Of course this means that your code won't work if 'foo' isn't
actually a Foo instance. So you lose a little generality, but you gain
a bit in conciceness of expression (IMHO). Note also that I don't use
dict.get, relying on the KeyError instead. Usually, my case for when no
function applies is complex enough to not be expressable in a lambda
anyway, so this is generally more appropriate for my code.

While I don't generally find that I need lambda, I'm not particularly
arguing against it here. I just thought it might be helpful to
demonstrate how this code might be written concicely without lambdas.

Steve

 
Reply With Quote
 
tanghaibao@gmail.com
Guest
Posts: n/a
 
      12-23-2004
This is NOT true. Functional programming, AFAIKC, is a cool thing, and
in-fashion, increases productivity & readability, and an indispensable
thing for a flexible scripting lang. The inline/lambda function is
feature is shared by many lang. which I frequently use,
MATLAB/R/Python/etc. Well, you can say apply() is 'deprecated' now,
(which is another functional thing I like), but I am still using it. I
am not a desperate person who uses higher-order function factory a lot,
but if lambda keyword is removed, I swear I will not use the python
anymore.

 
Reply With Quote
 
Craig Ringer
Guest
Posts: n/a
 
      12-23-2004
On Thu, 2004-12-23 at 12:47, Keith Dart wrote:
> On 2004-12-23, Stephen Thorne <> wrote:
> > Hi guys,
> >
> > I'm a little worried about the expected disappearance of lambda in
> > python3000. I've had my brain badly broken by functional programming
> > in the past, and I would hate to see things suddenly become harder
> > than they need to be.

>
> I use Python lambda quite a bit, and I don't understand the recent noise
> about problems with it, and its removal. I don't have a problem with
> lambdas.
>
> My personal gripe is this. I think the core language, as of 2.3 or 2.4
> is very good, has more features than most people will ever use, and they
> (Guido, et al.) can stop tinkering with it now and concentrate more on
> the standard libraries.


As someone working with the Python/C API, I'd have to argue that the
Python language may (I express no opinion on this) be "Done", but
CPython doesn't look like it is.

In my view a bunch of minor, but irritating, issues exist:

It's hard to consistently support Unicode in extension modules without
doing a lot of jumping through hoops. Unicode in docstrings is
particularly painful. This may not be a big deal for normal extension
modules, but when embedding Python it's a source of considerable
frustration. It's also not easy to make docstrings translatable.
Supporting an optional encoding argument for docstrings in the
PyMethodDef struct would help a lot, especially for docstrings returned
by translation mechanisms.

Py_NewInterpreter/Py_EndInterpreter used in the main thread doesn't
work with a threaded Python debug build, calling abort(). If a
non-threaded Python is used, or a non-debug build, they appear to work
fine. There are some indications on the mailing list that this is due to
Python's reliance on thread-local-storage for per-interpreter data, but
there are no suggestions on how to work around this or even solid
explanations of it. This sucks severely when embedding Python in Qt
applications, as GUI calls may only be made from the main thread but
subinterepreters may not be created in the main thread. It'd be
fantastic if the subinterpreter mechanism could be fleshed out a bit. I
looked at it, but my C voodo just isn't up there. The ability to run
scripts in private interpreters or subinterpreters (that are disposed of
when the script terminates) would be immensely useful when using Python
as a powerful glue/scripting language in GUI apps.

IMO the reference behaviour of functions in the C API could be
clearer. One often has to simply know, or refer to the docs, to tell
whether a particular call steals a reference or is reference neutral.
Take, for example, PyDict_SetItemString vs PyMapping_SetItemString . Is
it obvious that one of those steals a reference, and one is reference
neutral? Is there any obvious rationale behind this? I'm not overflowing
with useful suggestions about this, but I do think it'd be nice if there
was a way to more easily tell how functions behave in regard to
reference counts.

Const. I know there's a whole giant can of worms here, but even so -
some parts of the Python/C API take arguments that will almost always be
string literals, such as format values for Py_BuildValue and
PyArg_ParseTuple or the string arguments to Py*_(Get|Set)String calls.
Many of these are not declared const, though they're not passed on to
anywhere else. This means that especially in c++, one ends up doing a
lot of swearing and including a lot of ugly and unnecessary string
copies or const_cast<char*>()s to silence the compiler's whining. It
would be nice if functions in the Python/C API would declare arguments
(not return values) const if they do not pass them on and do not change
them.

Of course, all these are just my opinion in the end, but I'd still have
to argue that using Python from C could be a lot nicer than it is. The
API is still pretty good, but the solution of these issues would make it
a fair bit nicer again, especially for people embedding Python in apps
(a place were it can seriously excel as a scripting/extension/glue
language).

--
Craig Ringer

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-23-2004
wrote:
> Well, you can say apply() is 'deprecated' now,
> (which is another functional thing I like), but I am still using it.


Interesting. Could you explain why? Personally, I find the
*expr/**expr syntax much simpler, so I'd be interested in knowing what
motivates you to continue to use apply...

Steve
 
Reply With Quote
 
Craig Ringer
Guest
Posts: n/a
 
      12-23-2004
On Thu, 2004-12-23 at 15:21, wrote:
> This is NOT true. Functional programming, AFAIKC, is a cool thing, and
> in-fashion, increases productivity & readability, and an indispensable
> thing for a flexible scripting lang.


Couldn't agree more. One of the things I find most valuable about Python
is the ability to use functional style where it's the most appropriate
tool to solve a problem - WITHOUT being locked into a pure-functional
purist language where I have to fight the language to get other things
done.

> The inline/lambda function is
> feature is shared by many lang. which I frequently use,
> MATLAB/R/Python/etc. Well, you can say apply() is 'deprecated' now,
> (which is another functional thing I like), but I am still using it. I
> am not a desperate person who uses higher-order function factory a lot,
> but if lambda keyword is removed, I swear I will not use the python
> anymore.


I also make signficant use of Lambda functions, but less than I used to.
I've recently realised that they don't fit too well with Python's
indentation-is-significant syntax, and that in many cases my code is
much more readable through the use of a local def rather than a lambda.

In other words, I increasingly find that I prefer:

def myfunction(x):
return x**4
def mysecondfuntion(x):
return (x * 4 + x**2) / x
make_some_call(myfunction, mysecondfunction)

to:

make_some_call( lambda x: x**4, lambda x: (x * 4 + x**2) / x)

finding the former more readable. The function names are after all just
temporary local bindings of the function object to the name - no big
deal. Both the temporary function and the lambda can be used as
closures, have no impact outside the local function's scope, etc. I'd be
interested to know if there's anything more to it than this (with the
side note that just don't care if my temporary functions are anonymous
or not).

One of the things I love about Python is the ability to mix functional,
OO, and procedural style as appropriate. I can write a whole bunch of
functions that just return the result of list comprehensions, then use
them to operate on instances of an object from a procedural style main
function. In fact, that's often the cleanest way to solve the problem.
The fact that I have that option is something I really like.

As for apply(), while it is gone, the f(*args) extension syntax is now
available so I don't really see the issue. After all, these two are the
same:

def callfunc(function,args):
return apply(function,args)

and

def callfunc(function,args):
return function(*args)

its just an (IMO trivial) difference in syntax. I'd be interested in
knowing if there is in fact more to it than this.

--
Craig Ringer

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-23-2004
Craig Ringer wrote:

> It's hard to consistently support Unicode in extension modules without
> doing a lot of jumping through hoops. Unicode in docstrings is
> particularly painful. This may not be a big deal for normal extension
> modules, but when embedding Python it's a source of considerable
> frustration. It's also not easy to make docstrings translatable.
> Supporting an optional encoding argument for docstrings in the
> PyMethodDef struct would help a lot, especially for docstrings returned
> by translation mechanisms.


docstrings should be moved out of the C modules, and into resource
files. (possibly via macros and an extractor). when I ship programs
to users, I should be able to decide whether or not to include docstrings
without having to recompile the darn thing.

and yes, docstrings should support any encoding supported by python's
unicode system.

> Const. I know there's a whole giant can of worms here, but even so -
> some parts of the Python/C API take arguments that will almost always be
> string literals, such as format values for Py_BuildValue and
> PyArg_ParseTuple or the string arguments to Py*_(Get|Set)String calls.
> Many of these are not declared const, though they're not passed on to
> anywhere else. This means that especially in c++, one ends up doing a
> lot of swearing and including a lot of ugly and unnecessary string
> copies or const_cast<char*>()s to silence the compiler's whining. It
> would be nice if functions in the Python/C API would declare arguments
> (not return values) const if they do not pass them on and do not change
> them.


I think the only reason that this hasn't already been done is to reduce the
amount of swearing during the conversion process (both for the core developer
and C extension developers...).

</F>



 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      12-23-2004
Keith Dart <> wrote:

> My personal gripe is this. I think the core language, as of 2.3 or 2.4
> is very good, has more features than most people will ever use, and they


Indeed, it has _too many_ features. Look at the PEP about 3.0, and
you'll see that removing redundant features and regularizing a few
oddities is what it's meant to be all about.

> (Guido, et al.) can stop tinkering with it now and concentrate more on
> the standard libraries.


No doubt the standard library needs more work, particularly if you count
the built-ins as being part of the library (which, technically, they
are). Indeed, much of the redundancy previously mentioned is there
rather than in the core language strictly speaking -- e.g., all of the
things returning lists (from keys, values, items methods in dicts, to
the range built-in) mostly-duplicated with things returning iterators
(iterkeys, etc) or nearly so (xrange). These are things that can't
change in 2.5 to avoid breaking backwards compatibility. Other things,
where bw compat is not threatened, are no doubt going to be targeted in
2.5.


Alex
 
Reply With Quote
 
Alan Gauld
Guest
Posts: n/a
 
      12-23-2004
On Thu, 23 Dec 2004 14:13:28 +1000, Stephen Thorne
<> wrote:
> I'm a little worried about the expected disappearance of lambda in
> python3000. I've had my brain badly broken by functional programming
> in the past, and I would hate to see things suddenly become harder
> than they need to be.


Me too.
But its not only about becoming harder, I actually like the fact
that lamda exists as a tie in to the actual concept of an
anonymous function. When you read a math book on lambda calculus
its nice to transform those concepts directly to the language.

The current Pythonic lambda has its limitations, but being able
to explicitly create lambdas is a nice feature IMHO. Its much
better than having to create lots of one-off single use functions
with meaningless names.

It can't be that hard to maintain the lambda code, why not just
leave it there for the minority of us who like the concept?

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
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
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
Hi :D FRIENZ :D FOR ALL YOU FASHION FANZ OUT HERE :D :D URGENT n1kk1 Java 0 01-25-2010 03:01 AM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM
Switch statement (was: Lambda going out of fashion) Skip Montanaro Python 2 01-03-2005 02:25 PM



Advertisments