Re: Why is lambda allowed as a key in a dict?

2009-03-11 Thread Lie Ryan
Terry Reedy wrote: r wrote: On Mar 11, 3:40 pm, Craig Allen wrote: On Mar 10, 1:39 pm, Paul Rubin wrote: Identical strings don't necessarily have the same id: A more verbose way to put this is "Requesting a string with a value that is the same an an existi

Re: Why is lambda allowed as a key in a dict?

2009-03-11 Thread r
On Mar 11, 4:32 pm, Terry Reedy wrote: > Similarly, if one is populating a LARGE structure with duplicate values, > it may be worthwhile to cache values that are not cached by the interpreter. Thanks Terry, Actually i had no idea how Python handled strings(immutables) internally but after conside

Re: Why is lambda allowed as a key in a dict?

2009-03-11 Thread Terry Reedy
r wrote: On Mar 11, 3:40 pm, Craig Allen wrote: On Mar 10, 1:39 pm, Paul Rubin wrote: Identical strings don't necessarily have the same id: A more verbose way to put this is "Requesting a string with a value that is the same an an existing string does not n

Re: Why is lambda allowed as a key in a dict?

2009-03-11 Thread r
On Mar 11, 3:40 pm, Craig Allen wrote: > On Mar 10, 1:39 pm, Paul Rubin wrote: > > > Craig Allen writes: > > > it raises an interesting question about why doesn't it.  I can think > > > of practical answers to that, obviously, but in principle, if a > > > function c

Re: Why is lambda allowed as a key in a dict?

2009-03-11 Thread Craig Allen
On Mar 10, 1:39 pm, Paul Rubin wrote: > Craig Allen writes: > > it raises an interesting question about why doesn't it.  I can think > > of practical answers to that, obviously, but in principle, if a > > function compiles to exactly the same byte code, you obviously

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Gabriel Genellina
En Tue, 10 Mar 2009 21:23:54 -0200, Craig Allen escribió: I think the point is that function objects compare by object identity, so the two lambdas you use above are not equal even though they have the same code. it raises an interesting question about why doesn't it. I can think of prac

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Martin v. Löwis
> it raises an interesting question about why doesn't it. I can think > of practical answers to that, obviously, but in principle, if a > function compiles to exactly the same byte code, you obviously do not > need two copies of it, and like strings shouldn't an identical > function have the same

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Paul Rubin
Craig Allen writes: > it raises an interesting question about why doesn't it. I can think > of practical answers to that, obviously, but in principle, if a > function compiles to exactly the same byte code, you obviously do not > need two copies of it, and like strings shouldn't an identical > fu

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Craig Allen
> I think the point is that function objects compare by object identity, > so the two lambdas you use above are not equal even though they have the > same code. it raises an interesting question about why doesn't it. I can think of practical answers to that, obviously, but in principle, if a fun

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Gabriel Genellina
En Tue, 10 Mar 2009 13:00:18 -0200, Vito De Tullio escribió: MRAB wrote: >>> (lambda arg: arg) == (lambda arg: arg) False curious... I somehow thinked that, whereas (lambda: 0) is (lambda: 0) should be False (obviously) (lambda: 0) == (lambda: 0) could be True... maybe because `{}

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Vito De Tullio
MRAB wrote: > >>> (lambda arg: arg) == (lambda arg: arg) > False curious... I somehow thinked that, whereas >>> (lambda: 0) is (lambda: 0) should be False (obviously) >>> (lambda: 0) == (lambda: 0) could be True... maybe because `{} == {} and {} is not {}` (also for []) -- By ZeD -- http://

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Duncan Booth
Iain King wrote: > Sort of tangenitally; is there any real difference between the outcome > of the two following pieces of code? > > a = lambda x: x+2 > > def a(x): > return x+2 > Disassemble it to see. The functions themselves have identical code bytes, the only difference is the name o

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread S Arrowsmith
Iain King wrote: >Sort of tangenitally; is there any real difference between the outcome >of the two following pieces of code? > >a = lambda x: x+2 > >def a(x): >return x+2 a.__name__ As for why that matters, try a(None) and see which gives the more informative traceback. -- \S under

Re: Why is lambda allowed as a key in a dict?

2009-03-10 Thread Iain King
On Mar 10, 6:38 am, Daniel Fetchinson wrote: > On 3/9/09, bearophileh...@lycos.com wrote: > > > See here Daniel Fetchinson: > > >http://groups.google.com/group/comp.lang.python/browse_thread/thread/... > > > But be quite careful in using that stuff, it has some traps. > > Thanks a lot for all the

Re: Why is lambda allowed as a key in a dict?

2009-03-09 Thread Daniel Fetchinson
On 3/9/09, bearophileh...@lycos.com wrote: > See here Daniel Fetchinson: > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/a973de8f3562675c > > But be quite careful in using that stuff, it has some traps. Thanks a lot for all the helpful replies! Yes, I should name the unna

Re: Why is lambda allowed as a key in a dict?

2009-03-09 Thread bearophileHUGS
See here Daniel Fetchinson: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a973de8f3562675c But be quite careful in using that stuff, it has some traps. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Why is lambda allowed as a key in a dict?

2009-03-09 Thread Ryan Kelly
>Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26) > [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> x = { } > >>> x[lambda arg: arg] = 5 > >>> x[lambda arg: arg] > Traceback (most recent call last): > File "", line 1

Re: Why is lambda allowed as a key in a dict?

2009-03-09 Thread MRAB
Daniel Fetchinson wrote: Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26) [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2 Type "help", "copyright", "credits" or "license" for more information. x = { } x[lambda arg: arg] = 5 x[lambda arg: arg] Traceback (most recent call last): File "", line

Re: Why is lambda allowed as a key in a dict?

2009-03-09 Thread David Stanek
On Mon, Mar 9, 2009 at 11:07 PM, Daniel Fetchinson wrote: > Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26) > [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. x = { } x[lambda arg: arg] = 5 x[lambda arg: arg]

Why is lambda allowed as a key in a dict?

2009-03-09 Thread Daniel Fetchinson
Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26) [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x = { } >>> x[lambda arg: arg] = 5 >>> x[lambda arg: arg] Traceback (most recent call last): File "", line 1, in KeyError: