473,881 Members | 1,718 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
57 3640

"Egor Bolonev" <eb******@mail. ru> wrote in message
news:op******** ******@theurs.o ctopusnet.lan.. .
why functions created with lambda forms cannot contain statements?


Because lambda was only ever intended to be an inline abbreviation of
simple one-use functions whose body consists of 'return <expression>' . It
is clear to me that the word 'lambda' was a mistake since it engenders
expectations of more than that from too many people, such as you.

Terry J. Reedy

Jul 18 '05 #11

"Paul Rubin" <"http://phr.cx"@NOSPAM. invalid> wrote in message
news:7x******** ****@ruckus.bro uhaha.com...
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.


No, not same difference. A list method would only operate on lists, as is
true of all list methods. Being a function lets it work for any iterable,
as is true of any function of iterable. Big difference. And consistent.
One could argue though that it should have been put into itermethods module
instead of builtins.

Terry J. Reedy

Jul 18 '05 #12
Op 2005-01-13, hanz schreef <ha******@yahoo .com.au>:

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.


Sorry, someone already did. If I recall correctly it
was Alex Martelli.

--
Antoon Pardon
Jul 18 '05 #13
Antoon Pardon wrote:
Op 2005-01-13, hanz schreef <ha******@yahoo .com.au>:
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.

Of course, unless that reference is in the global scope of the __main__
module its lifetime will be transient anyway. If the reference is stored
in a function's local variable then unless its value is returned from
the function it will become available for garbage collection when the
function returns.
Nobody will tell you that it's bad.

Sorry, someone already did. If I recall correctly it
was Alex Martelli.

"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken. Besides which, if you don't understand the language
environment, rules alone will do you very little good. Try to focus a
little more on principles and a little less on minutiae.

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

Jul 18 '05 #14
Op 2005-01-14, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
Op 2005-01-13, hanz schreef <ha******@yahoo .com.au>:
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.
Of course, unless that reference is in the global scope of the __main__
module its lifetime will be transient anyway. If the reference is stored
in a function's local variable then unless its value is returned from
the function it will become available for garbage collection when the
function returns.
Nobody will tell you that it's bad.

Sorry, someone already did. If I recall correctly it
was Alex Martelli.

"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.


Like only use immutables as dictionary keys.
Besides which, if you don't understand the language
environment, rules alone will do you very little good. Try to focus a
little more on principles and a little less on minutiae.


And what are the difference between those two?

Sometimes I get the impression that everything is a principle until
one personnaly finds the need to break it. After that it is a rule.

--
Antoon Pardon
Jul 18 '05 #15
Antoon Pardon wrote:
[...]
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.

Like only use immutables as dictionary keys.

Fair enough, but don;t go advising newbies to do this.
Besides which, if you don't understand the language
environment , rules alone will do you very little good. Try to focus a
little more on principles and a little less on minutiae.

And what are the difference between those two?

Sometimes I get the impression that everything is a principle until
one personnaly finds the need to break it. After that it is a rule.

Principle: "Ten angels can dance on the head of a pin".

Minutiae: "Well, if they'd all recently been on a diet and they hold on
to each other very carefully you can get 10.3. I got eleven once by
adding a hash function".

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #16
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
[...]
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.

Like only use immutables as dictionary keys.

Fair enough, but don;t go advising newbies to do this.


How about something like this.

Because of the extra precautions one has to take when
using mutables as hash keys, we advise newbies
to stick with immutable keys until they have gathered
enough knowledge and experience to adequatly weight
the pro and cons of a mutable key solution against
an immutable key solution.

--
Antoon Pardon
Jul 18 '05 #17
Antoon Pardon wrote:
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
[...]
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.
Like only use immutables as dictionary keys.


Fair enough, but don;t go advising newbies to do this.

How about something like this.

Because of the extra precautions one has to take when
using mutables as hash keys, we advise newbies
to stick with immutable keys until they have gathered
enough knowledge and experience to adequatly weight
the pro and cons of a mutable key solution against
an immutable key solution.

There you go with the minutiae again. How about:

"Don't use mutables as hash keys"?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #18
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
[...]

>"A foolish consistency is the hobgoblin of little minds". Rules are made
>to be broken.
Like only use immutables as dictionary keys.
Fair enough, but don;t go advising newbies to do this.

How about something like this.

Because of the extra precautions one has to take when
using mutables as hash keys, we advise newbies
to stick with immutable keys until they have gathered
enough knowledge and experience to adequatly weight
the pro and cons of a mutable key solution against
an immutable key solution.

There you go with the minutiae again. How about:

"Don't use mutables as hash keys"?


That sounds too dogmatic to my ears. I also find it
too selective. The problem with mutables as dictionary
keys is not specific to dictionaries. Everywhere you
have mutables in a container, it is possible that
mutating the object in the container will cause
problem. Heck even using mutables as arguments can
cause trouble. Why else the specific advice against

def foo(p = [])

type of arguments. So should we adopt the principles:

Don't use mutables in containers

Don't use mutables as default values for parameters

Don't use mutables as arguments.

Don't assign one mutable to an other.
I don't see a big difference between these principles
and the hash key principle, so in the end may be we
should just stick with the more general principle:

Don't use mutables!
and be done with it.

--
Antoon Pardon
Jul 18 '05 #19
In article <sl************ ********@rcpc42 .vub.ac.be>,
Antoon Pardon <ap*****@forel. vub.ac.be> wrote:
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
There you go with the minutiae again. How about:

"Don't use mutables as hash keys"?


That sounds too dogmatic to my ears. I also find it
too selective. The problem with mutables as dictionary
keys is not specific to dictionaries. Everywhere you
have mutables in a container, it is possible that
mutating the object in the container will cause
problem.


The main difference is: if you mutate a dict key you *always* have a
problem. So don't do that. Mutating (say) a list item *only* is a
problem if you (say) need that list to remain sorted. Lists don't
magically remain sorted, so people generally sort it before they do some
operation that expects a sorted list.
Heck even using mutables as arguments can
cause trouble. Why else the specific advice against

def foo(p = [])

type of arguments. So should we adopt the principles:

Don't use mutables in containers
Nonsense.
Don't use mutables as default values for parameters
That's good advice in general.
Don't use mutables as arguments.
Nonsense.
Don't assign one mutable to an other.
Nonsense. Some newbies get surprised by Python's assignment-doesn't-copy
semantics, but it's such basic knowledge (as well as a useful feature)
that I simply don't understand you saying this.
I don't see a big difference between these principles
and the hash key principle,
Than you haven't looked hard enough.
so in the end may be we
should just stick with the more general principle:

Don't use mutables!
and be done with it.


Ok, so you're indeed a troll.

Just
Jul 18 '05 #20

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

Similar topics

53
3719
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
3437
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
3517
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
8957
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
2591
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
2638
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
5353
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
2170
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
1872
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
2635
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
9926
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9775
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
10396
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
9550
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 projectplanning, coding, testing, and deploymentwithout 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
7105
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
5780
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...
0
5974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
3
3221
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.