Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > lambda

Reply
Thread Tools

lambda

 
 
Egor Bolonev
Guest
Posts: n/a
 
      01-13-2005
why functions created with lambda forms cannot contain statements?

how to get unnamed function with statements?
 
Reply With Quote
 
 
 
 
Daniel Dittmar
Guest
Posts: n/a
 
      01-13-2005
Egor Bolonev wrote:
> why functions created with lambda forms cannot contain statements?


syntax/technical answer: because lambda is an expression and it is not
obvious how the syntax for 'statement inside expression' should be

'Python is perfect' answer: if a function contains more than an
expression, then it's complex enough to require a name

> how to get unnamed function with statements?


You can't. See various threads about Smalltalk/Ruby-like blocks and the
recent one about the 'where' keyword for proposals to change this

Daniel
 
Reply With Quote
 
 
 
 
Tim Leslie
Guest
Posts: n/a
 
      01-13-2005
Because if it takes more than a single line it deserves a name. Also,
if you have more than one line in this function, how do you plan to
reference it if not by name?

Tim


On Thu, 13 Jan 2005 20:53:09 +1000, Egor Bolonev <(E-Mail Removed)> wrote:
> why functions created with lambda forms cannot contain statements?
>
> how to get unnamed function with statements?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      01-13-2005
Op 2005-01-13, Tim Leslie schreef <(E-Mail Removed)>:
> Because if it takes more than a single line it deserves a name.


So if I have a call with an expression that takes more than
one line, I should assign the expression to a variable and
use the variable in the call?

But wait if I do that, people will tell me how bad that it
is, because it will keep a reference to the value which
will prevent the garbage collector from harvesting this
memory.

Besides python allows more than one statement on the same line.

--
Antoon Pardon
 
Reply With Quote
 
hanz
Guest
Posts: n/a
 
      01-13-2005

Antoon Pardon wrote:
> So if I have a call with an expression that takes more than
> one line, I should assign the expression to a variable and
> use the variable in the call?


Yes, that's sometimes a good practice and can clarify
the call.

> But wait if I do that, people will tell me how bad that it
> is, because it will keep a reference to the value which
> will prevent the garbage collector from harvesting this
> memory.


Nobody will tell you that it's bad. Python was never
about super performance, but about readability.
Besides, using such temporaries won't consume much
memory (relatively).

> Besides python allows more than one statement on the same line.

But it's discouraged in general.

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-13-2005
"hanz" <(E-Mail Removed)> writes:
> > But wait if I do that, people will tell me how bad that it is,
> > because it will keep a reference to the value which will prevent
> > the garbage collector from harvesting this memory.

>
> Nobody will tell you that it's bad. Python was never about super
> performance, but about readability. Besides, using such temporaries
> won't consume much memory (relatively).


That completely depends on the objects in question. Compare

temp = all_posters[:]
temp.sort()
top_five_posters = temp[-5:]

to:

top_five_posters = all_posters.sorted()[-5:]

which became possible only when .sorted() was added to Python 2.4.

This is another reason it would be nice to be able to create very
temporary scopes.
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      01-13-2005
Paul Rubin wrote:
>
> That completely depends on the objects in question. Compare
>
> temp = all_posters[:]
> temp.sort()
> top_five_posters = temp[-5:]
>
> to:
>
> top_five_posters = all_posters.sorted()[-5:]
>
> which became possible only when .sorted() was added to Python 2.4.


I believe you mean "when sorted() was added to Python 2.4":

py> ['d', 'b', 'c', 'a'].sorted()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'list' object has no attribute 'sorted'
py> sorted(['d', 'b', 'c', 'a'])
['a', 'b', 'c', 'd']

Note that sorted is a builtin function, not a method of a list object.
It takes any iterable and creates a sorted list from it. Basically the
equivalent of:

def sorted(iterable):
result = list(iterable)
result.sort()
return result

Steve
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-13-2005
Steven Bethard <(E-Mail Removed)> writes:
> Note that sorted is a builtin function, not a method of a list
> object.


Oh, same difference. I thought it was a method because I'm not using
2.4 yet. The result is the same, other than that having it as a
function instead of a method is another inconsistency to remember.
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-13-2005
Paul Rubin wrote:

>> Note that sorted is a builtin function, not a method of a list
>> object.

>
> Oh, same difference. I thought it was a method because I'm not using
> 2.4 yet. The result is the same


nope. sorted works on any kind of sequence, including forward-only
iterators. sorted(open(filename)) works just fine, for example.

> other than that having it as a function instead of a method is another
> inconsistency to remember


I suspect that you don't really understand how sequences and iterators
work in Python...

</F>



 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-13-2005
"Fredrik Lundh" <(E-Mail Removed)> writes:
> > Oh, same difference. I thought it was a method because I'm not using
> > 2.4 yet. The result is the same

>
> nope. sorted works on any kind of sequence, including forward-only
> iterators. sorted(open(filename)) works just fine, for example.


Oh cool. However I meant the result is the same in my example, where
assigning the temporary result to a variable stopped memory from
getting reclaimed until after the function exits.
 
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
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
A critic of Guido's blog on Python's lambda Xah Lee Java 177 05-23-2006 07:11 PM
A Lambda Logo Tour Xah Lee Java 5 04-07-2006 04:07 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



Advertisments