473,695 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lambda

why functions created with lambda forms cannot contain statements?

how to get unnamed function with statements?
Jul 18 '05 #1
57 3595
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
Jul 18 '05 #2
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 <eb******@mail. ru> 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

Jul 18 '05 #3
Op 2005-01-13, Tim Leslie schreef <ti********@gma il.com>:
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
Jul 18 '05 #4

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.

Jul 18 '05 #5
"hanz" <ha******@yahoo .com.au> 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_poster s = temp[-5:]

to:

top_five_poster s = all_posters.sor ted()[-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.
Jul 18 '05 #6
Paul Rubin wrote:

That completely depends on the objects in question. Compare

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

to:

top_five_poster s = all_posters.sor ted()[-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 "<interacti ve 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
Jul 18 '05 #7
Steven Bethard <st************ @gmail.com> 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.
Jul 18 '05 #8
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(fil ename)) 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>

Jul 18 '05 #9
"Fredrik Lundh" <fr*****@python ware.com> 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(fil ename)) 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.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
3673
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
63
3388
by: Stephen Thorne | last post by:
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.
26
3487
by: Steven Bethard | last post by:
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 statements (<name> = lambda <args>: <expr> usage) These are lambdas used when a lambda wasn't needed -- an anonymous function was created with...
181
8797
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
25
2560
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until run-time: someobject.newfunc = lambda x: f(foo, x) The reason a nested function doesn't work for this is that it is, well, dynamic. I don't know how many times or with what foo's this will be done.
4
2625
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with other mathematical symbols. In my mind, i imagine that those obscure math
23
5313
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
5
2157
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a bit complicated so any explanation would be appreciated. Thanks
21
1845
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the following function? def addn(n): return lambda x,inc=n: x+inc if i do addn(5) it returns
1
2625
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET boost::lambda::lambda_ functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
0
8562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8830
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8820
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7655
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4339
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2265
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.