Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Mark Lawrence
On 25/03/2014 01:45, Steven D'Aprano wrote: (1) People who just want to get the job done, without learning a bunch of theory, *won't care* how their sort key function is written. They're looking for a recipe that they can copy and paste, and whether you write it like this:

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Chris Angelico
On Tue, Mar 25, 2014 at 12:45 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Programming is a skill, like writing iambic pentameter. Should liberal arts courses ban the use of iambic pentameter by poets because some people find it confusing and can't count syllables or tell the

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Mark H Harris
On 3/24/14 8:20 PM, Terry Reedy wrote: On 3/24/2014 7:56 PM, Mark H Harris wrote: the list which is used for each of the adder[] functions created. Wrong. Functions look up global and nonlocal names, such as n, when the function is called. hi Terry, yeah, I know; this is what's *wrong*

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Mark H Harris
On 3/24/14 8:45 PM, Steven D'Aprano wrote: Your insistence that lambda is confusing is awfully condescending. People are not as dumb as you insist, and they are perfectly capable of learning lambda without a comp sci degree. Like any technical jargon, there is vocabulary and meaning to learn,

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Steven D'Aprano
On Tue, 25 Mar 2014 06:57:19 +1100, Chris Angelico wrote: Can you imagine a high level language without a simple notation for variable assignment? Certainly not. I don't know... what a stack based or concatenative language like Forth, Postscript, Joy or Factor count as high-level? I'm pretty

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-24 Thread Chris Angelico
On Tue, Mar 25, 2014 at 4:28 PM, Steven D'Aprano st...@pearwood.info wrote: On Tue, 25 Mar 2014 06:57:19 +1100, Chris Angelico wrote: Can you imagine a high level language without a simple notation for variable assignment? Certainly not. I don't know... what a stack based or concatenative

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Rhodri James
On Sun, 23 Mar 2014 02:46:28 -, Ian Kelly ian.g.ke...@gmail.com wrote: On Sat, Mar 22, 2014 at 6:32 PM, Rhodri James rho...@wildebst.org.uk wrote: On Sat, 22 Mar 2014 05:26:26 -, Rustom Mody rustompm...@gmail.com wrote: Well almost... Except that the 'loop' I am talking of is one

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Chris Angelico
On Mon, Mar 24, 2014 at 1:35 PM, Rhodri James rho...@wildebst.org.uk wrote: Would you not consider this to be declarative? x = [1, 2, 3] I'm not sure I would. I look at that line of code and think of it as Create a list..., very much in an imperative manner. Then again, compared with

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Chris Angelico
On Mon, Mar 24, 2014 at 1:35 PM, Rhodri James rho...@wildebst.org.uk wrote: I'm not sure I would. I look at that line of code and think of it as Create a list..., very much in an imperative manner. Then again, compared with C structs and typedefs and actual honest-to-God type declarations,

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Rustom Mody
On Monday, March 24, 2014 8:57:32 AM UTC+5:30, Chris Angelico wrote: On Mon, Mar 24, 2014 at 1:35 PM, Rhodri James wrote: Would you not consider this to be declarative? x = [1, 2, 3] I'm not sure I would. I look at that line of code and think of it as Create a list..., very much in

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Chris Angelico
On Mon, Mar 24, 2014 at 3:14 PM, Rustom Mody rustompm...@gmail.com wrote: Neat! So I play around... Change it to [(x,y) for x in range(1,1) for y in range(1,1)] and I dont have an answer but a thrashing machine!! (*) Yes, because you used square brackets, which means that the list has

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-23 Thread Mark H Harris
On 3/22/14 4:46 AM, Steven D'Aprano wrote: On Fri, 21 Mar 2014 23:51:38 -0500, Mark H Harris wrote: Lambda is a problem, if only because it causes confusion. What's the problem? Glad you asked. The constructs DO NOT work the way most people would expect them to, having limited knowledge of

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Ian Kelly
On Fri, Mar 21, 2014 at 8:06 PM, Rustom Mody rustompm...@gmail.com wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in a comprehension Because Python as a language only has

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Steven D'Aprano
On Fri, 21 Mar 2014 23:51:38 -0500, Mark H Harris wrote: Lambda is a problem, if only because it causes confusion. What's the problem? Glad you asked. The constructs DO NOT work the way most people would expect them to, having limited knowledge of python! Why is that a problem? Would you

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Marko Rauhamaa
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info: This makes perfect sense: by the time you call the functions, the name x has been rebound to the value 3. [...] Now I'm not sure precisely how Haskell implements this trick, but it suggests to me that it creates a different closure

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Marko Rauhamaa
Ian Kelly ian.g.ke...@gmail.com: You can get the desired effect by adding a layer of indirection: fl = [(lambda x: lambda y: x+y)(x) for x in [1,2,3]] A trick to remember! Variable lifetime reduction by function invocation. Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Mark Lawrence
On 22/03/2014 02:06, Rustom Mody wrote: The same in haskell: Prelude let fl = [\ y - x + y | x - [1,2,3]] Prelude [(fl!!i) 0 | i- [0,1,2]] [1,2,3] My really big complaint about Python is that it's nothing like CORAL 66. I think I'll raise this on python ideas in an attempt to get this

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Rustom Mody
On Saturday, March 22, 2014 2:39:56 PM UTC+5:30, Ian wrote: On Fri, Mar 21, 2014 at 8:06 PM, Rustom Mody wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in a

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Mark Lawrence
On 22/03/2014 09:09, Ian Kelly wrote: On Fri, Mar 21, 2014 at 8:06 PM, Rustom Mody rustompm...@gmail.com wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in a comprehension

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Marko Rauhamaa
Mark Lawrence breamore...@yahoo.co.uk: On 22/03/2014 09:09, Ian Kelly wrote: Because Python as a language only has the concept of assignment, not binding. I think it would be weird and confusing if variables worked this way in comprehensions and nowhere else. My understanding has always

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Rustom Mody
The foll is fairly standard fare in denotational semantics -- please excuse the length! In order to understand (formally) the concept of 'variable' we need to have at the least a concept of name(or identifier) - value mapping. This mapping is called an 'environment' If we stop at that we get the

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread vasudevram
Thanks to all those who answered. - Vasudev -- https://mail.python.org/mailman/listinfo/python-list

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Rhodri James
On Sat, 22 Mar 2014 05:26:26 -, Rustom Mody rustompm...@gmail.com wrote: Well almost... Except that the 'loop' I am talking of is one of def loop(): return [yield (lambda: x) for x in [1,2,3]] or return (yield (lambda: x) for x in [1,2,3]) or just plain ol (lambda x: for x

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Ian Kelly
On Sat, Mar 22, 2014 at 6:32 PM, Rhodri James rho...@wildebst.org.uk wrote: On Sat, 22 Mar 2014 05:26:26 -, Rustom Mody rustompm...@gmail.com wrote: Well almost... Except that the 'loop' I am talking of is one of def loop(): return [yield (lambda: x) for x in [1,2,3]] or

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Rustom Mody
On Sunday, March 23, 2014 8:16:28 AM UTC+5:30, Ian wrote: On Sat, Mar 22, 2014 at 6:32 PM, Rhodri James wrote: wrote: Well almost... Except that the 'loop' I am talking of is one of def loop(): return [yield (lambda: x) for x in [1,2,3]] or return (yield (lambda: x) for x

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-22 Thread Ian Kelly
On Sat, Mar 22, 2014 at 9:16 PM, Rustom Mody rustompm...@gmail.com wrote: [I am not completely sure whether the following can be proved/is true] 1. One can change lambda's closure rules which would amount to significant complexity for relatively little gain 2. One can change comprehension

Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread vasudevram
Hi list, Can anyone - maybe one of the Python language core team, or someone with knowledge of the internals of Python - can explain why this code works, and whether the different occurrences of the name x in the expression, are in different scopes or not? : x = [[1,2], [3,4], [5,6]] [x

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 2:12:53 AM UTC+5:30, vasudevram wrote: Hi list, Can anyone - maybe one of the Python language core team, or someone with knowledge of the internals of Python - can explain why this code works, and whether the different occurrences of the name x in the expression,

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread vasudevram
On Saturday, March 22, 2014 2:24:00 AM UTC+5:30, Rustom Mody wrote: Lets try without comprehending comprehensions :-) x=[[1,2],[3,4]] for x in x: ... for x in x: ... print x ... 1 2 3 4 Nice and all, thanks, but doesn't answer the question. --

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 2:26:09 AM UTC+5:30, vasudevram wrote: On Saturday, March 22, 2014 2:24:00 AM UTC+5:30, Rustom Mody wrote: Lets try without comprehending comprehensions :-) x=[[1,2],[3,4]] for x in x: ... for x in x: ... print x ... 1 2 3 4 Nice and

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Ian Kelly
On Fri, Mar 21, 2014 at 3:09 PM, Rustom Mody rustompm...@gmail.com wrote: A 'for' introduces a scope: This is false. x = 42 for x in [1,2,3]: ... print x ... 1 2 3 No sign of the 42 --v ie the outer x -- inside because of scope Try printing x again *after* the for loop: x = 42

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Gregory Ewing
Rustom Mody wrote: A 'for' introduces a scope: No, it doesn't! x = 42 for x in [1,2,3]: ... print x ... 1 2 3 No sign of the 42 --v ie the outer x -- inside because of scope You're right that there's no sign of the 42, but it's *not* because of scope, as you'll see if you do one

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 3:00:10 AM UTC+5:30, Ian wrote: On Fri, Mar 21, 2014 at 3:09 PM, Rustom Mody wrote: A 'for' introduces a scope: This is false. And On Saturday, March 22, 2014 3:04:48 AM UTC+5:30, Gregory Ewing wrote: A 'for' introduces a scope: No, it doesn't! Ha --

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Chris Angelico
On Sat, Mar 22, 2014 at 1:06 PM, Rustom Mody rustompm...@gmail.com wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in a comprehension fl = [lambda y : x+y for x in

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 8:11:27 AM UTC+5:30, Chris Angelico wrote: On Sat, Mar 22, 2014 at 1:06 PM, Rustom Mody wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Steven D'Aprano
On Fri, 21 Mar 2014 19:06:06 -0700, Rustom Mody wrote: Two: A comprehension variable is not bound but reassigned across the comprehension. This problem remains in python3 and causes weird behavior when lambdas are put in a comprehension I don't know why you say the behaviour in Python is a

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Chris Angelico
On Sat, Mar 22, 2014 at 3:39 PM, Rustom Mody rustompm...@gmail.com wrote: So if that's not going to be broken, how is this fundamentally different? def func_loop(): for x in 1,2,3: yield (lambda: x) Thats using a for-loop A 'for' in a comprehension carries a different

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Mark H Harris
On 3/21/14 11:39 PM, Rustom Mody wrote: Given fl = [lambda y : x+y for x in [1,2,3]] It means: def rec(l): if not l: return [] else: x,ll = l[0],l[1:] return [lambda y: x + y] + rec(ll) followed by fl = rec([1,2,3]) Naturally a reasonable *implementation* would

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Chris Angelico
On Sat, Mar 22, 2014 at 3:47 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Now I'm not sure precisely how Haskell implements this trick, but it suggests to me that it creates a different closure each time around the loop of the comprehension. That could end up being very

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 10:21:13 AM UTC+5:30, Chris Angelico wrote: On Sat, Mar 22, 2014 at 3:39 PM, Rustom Mody wrote: So if that's not going to be broken, how is this fundamentally different? def func_loop(): for x in 1,2,3: yield (lambda: x) Thats using a for-loop A

Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

2014-03-21 Thread Rustom Mody
On Saturday, March 22, 2014 10:21:13 AM UTC+5:30, Chris Angelico wrote: On Sat, Mar 22, 2014 at 3:39 PM, Rustom Mody wrote: So if that's not going to be broken, how is this fundamentally different? def func_loop(): for x in 1,2,3: yield (lambda: x) Thats using a for-loop

<    1   2   3