473,898 Members | 2,803 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 3646
Antoon Pardon wrote:
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.

http://redwing.hutman.net/~mreed/war...ssrebutter.htm

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 #21
Op 2005-01-17, Just schreef <ju**@xs4all.nl >:
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.


That is not true. It is a problem every time I expect the list
items to remain the same.
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.


Well it is this same sematics that causes the problems with
mutable dictionary keys. If it is such basic knowledge then what
is the problem with mutable dictionary keys?
I don't see a big difference between these principles
and the hash key principle,


Than you haven't looked hard enough.


All of these can get unexpected behaviour because of the
assignment-doesn't-copy semantics. The same semantics
that can cause problems if you work with mutable dictionary
keys.
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.


The problems with mutables as dictionary keys is just one
particulary case of the problems you can have when your
assignmentxs semantics just creates a new reference to the
same object. As such it is no different from the problem
of mutating one object and finding out this object was
also reference through an other name you expected to remain
the same or finding out this object was also in a list you
expected to remain stable etc.

--
Antoon Pardon
Jul 18 '05 #22
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:
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.

http://redwing.hutman.net/~mreed/war...ssrebutter.htm

regards
Steve


I you want to play it like this

http://redwing.hutman.net/~mreed/war...tm/crybaby.htm

--
Antoon Pardon
Jul 18 '05 #23
On Mon, Jan 17, 2005 at 11:41:20AM +0000, 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.


knowledgeable and experienced users know when to ignore the rules.

--
John Lenton (jo**@grulic.or g.ar) -- Random fortune:
Una buena gran parte del arte del bien hablar consiste en saber mentir con
gracia.
-- Erasmo de Rotterdam. (1469-1536).

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFB69asgPq u395ykGsRAkNNAJ wPy4YOsAUoUHWTJ c6M1vWqdgquIwCd FCaf
LokGmkEUZL/0ezwbzMpRScc=
=XKjw
-----END PGP SIGNATURE-----

Jul 18 '05 #24
In article <sl************ ********@rcpc42 .vub.ac.be>,
Antoon Pardon <ap*****@forel. vub.ac.be> wrote:
I don't see a big difference between these principles
and the hash key principle,


Than you haven't looked hard enough.


All of these can get unexpected behaviour because of the
assignment-doesn't-copy semantics. The same semantics
that can cause problems if you work with mutable dictionary
keys.


Again, the difference is:

1. assigning mutable objects *can* cause unexpected behavior
(however, it's a useful feature, everyone using Python
for longer than a day or two knows this, and then it's
*expected* behavior.

2. mutating dict keys *does* *always* cause problems.
(unless you use an identity hash/cmp)

It's nonsense to forbid 1) since it's a useful feature. It's useful to
forbid ("discourage ") 2) since mutating dict keys is seldom useful (and
when it is, Python lets you support it in your own objects).

Just
Jul 18 '05 #25
Op 2005-01-17, John Lenton schreef <jo**@grulic.or g.ar>:


--vni90+aGYgRvsTu O
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Jan 17, 2005 at 11:41:20AM +0000, 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 ma= de=20 >>>to be broken.
>>=20
>>=20
>> Like only use immutables as dictionary keys.
>>=20
> Fair enough, but don;t go advising newbies to do this.

=20
How about something like this.
=20
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.


knowledgeable and experienced users know when to ignore the rules.


Then why seems there to be so few acknowledgement that these rules
may indeed be broken by users. My experience is that anyone who suggests
so runs the risk of being branded a (python) heretic.

--
Antoon Pardon
Jul 18 '05 #26
Antoon Pardon wrote:
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:

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.


http://redwing.hutman.net/~mreed/war...ssrebutter.htm

regards
Steve

I you want to play it like this

http://redwing.hutman.net/~mreed/war...tm/crybaby.htm

Hey, I'm not crying, or trying to play it any particular way. It's a
free Internet, and you are (more than) welcome to your opinions.
Personally I'd say I'm actually more of a combination of
http://redwing.hutman.net/~mreed/war...m/diplomat.htm and
http://redwing.hutman.net/~mreed/war.../evilclown.htm, with added
touches of http://redwing.hutman.net/~mreed/warriorshtm/garble.htm and
possibly http://redwing.hutman.net/~mreed/war...oxicgranny.htm

Whereas I also detect touches of
http://redwing.hutman.net/~mreed/war...rouscranus.htm in your
posts. It must be nice to *never* be wrong :-). Your resemblance to
http://redwing.hutman.net/~mreed/war...filibuster.htm, however,
makes me wish you could be right rather more succinctly.

Mostly, though, I was trying to say that I found your nitpicking
insistence on terminological exactitude, even when giving advice to
those new to the language, both inappropriate and tedious in the extreme.

Have a nice day :-)

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 #27
On Mon, 2005-01-17 at 12:15 -0300, John Lenton wrote:
knowledgeable and experienced users know when to ignore the rules.


+1 QOTW

One of the nice things is that Python permits you to do exactly that
where appropriate while avoiding forcing you to do gruesome things to
get a job done.

I think the classic example of your statement is the use of 'goto' in C
and C++ code. Don't use goto - except when there's no other sensible way
to make your code clear. For example, goto and Py_XECREF seem to be very
handy for cleanup after detecting an exception when working with the
Python/C API.

That said, I do think "the rules" deserve consideration and respect -
they're usually there because of many others' experience over time. It's
interesting to learn those lessons first hand, but it's nice to be able
to avoid repeating every single one of them.

--
Craig Ringer

Jul 18 '05 #28
Op 2005-01-17, Just schreef <ju**@xs4all.nl >:
In article <sl************ ********@rcpc42 .vub.ac.be>,
Antoon Pardon <ap*****@forel. vub.ac.be> wrote:
>> I don't see a big difference between these principles
>> and the hash key principle,
>
> Than you haven't looked hard enough.
All of these can get unexpected behaviour because of the
assignment-doesn't-copy semantics. The same semantics
that can cause problems if you work with mutable dictionary
keys.


Again, the difference is:

1. assigning mutable objects *can* cause unexpected behavior
(however, it's a useful feature, everyone using Python
for longer than a day or two knows this, and then it's
*expected* behavior.

2. mutating dict keys *does* *always* cause problems.
(unless you use an identity hash/cmp)


3 mutating an item in a sorted list *does* *always* cause problems

4 mutating an item in a heap queue *does* *always* cause problems
It's nonsense to forbid 1) since it's a useful feature. It's useful to
forbid ("discourage ") 2) since mutating dict keys is seldom useful (and
when it is, Python lets you support it in your own objects).


Yes mutating dict keys is seldom usefull. But again you are conflating
mutable dict keys with mutating dict keys. No mutables as dict keys
means you can't mutate the object even if it is not a key. But whether
assigning mutable objects is usefull or not doesn't depend on whether
the object will also be usefull as a key or not. You treat this as
if an obejct is always a key or never and then decide mutable objects
as dict keys is not usefull because mutating keys is not usefull.

More specific the Decimal class is mutable and usable as dict key.

--
Antoon Pardon
Jul 18 '05 #29
Op 2005-01-17, Steve Holden schreef <st***@holdenwe b.com>:
Antoon Pardon wrote:

Mostly, though, I was trying to say that I found your nitpicking
insistence on terminological exactitude, even when giving advice to
those new to the language, both inappropriate and tedious in the extreme.


I think it is appropiate because not all new to the language are new to
programming and even newbees have a right to know how it really is. Otherwise
after some time you get experienced users who don't know the fact. In
this case for example there are a number of people who flat out assert that
muatble dict keys in pyhthon is impossible.

--
Antoon Pardon
Jul 18 '05 #30

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

Similar topics

53
3720
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
3443
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
3518
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
8962
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
2639
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
5354
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
2636
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
9840
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
10857
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
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
9661
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...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
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
5880
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
4706
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
3305
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.