Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python is Considered Harmful

4 views
Skip to first unread message

mik...@ziplip.com

unread,
Oct 27, 2003, 1:25:41 AM10/27/03
to
Ladies and Gentlemen,

I present to you the final and ultimate proof of Python's
brain-damage:

As you may remember,

flist = []

for i in range(3)
f = lambda x: x + i
flist.append(f)

[f(1) for f in flist]

produces [3, 3, 3] in Python.

In Haskell, we would express this as follows:

map (\f -> f 1) [\x -> x + i | i <- [0..2]]

This, of course, evaluates to the expected [1, 2, 3]

As you might have heard, the ever so generous GvR allowed us write
the same in Python (literal translation, really):

map(lambda f: f(1), [lambda x: x + 1 for i in range(3)])

What do you think this evaluates to: also [1, 2, 3] or [3, 3, 3]
as before?

Guess again, it's [2, 2, 2] !

ROTFLMFAO!

Pythonista, you are all very welcome to learn Haskell.
You will find the syntax very familiar. Haskell is short
too (compare one line above that gives the correct
result to several Python lines that surprise you)

All this Python bashing is starting to feel like mocking
a retarded child...

420

Darius

unread,
Oct 27, 2003, 1:44:12 AM10/27/03
to
On Sun, 26 Oct 2003 22:25:41 -0800 (PST)
mik...@ziplip.com wrote:

Ladies and Gentlemen,

I present to you the final and ultimate proof of mike420's
brain-damage:

In Haskell, we would express this as follows:

map (\f -> f 1) [\x -> x + 1 | i <- [0..2]]

This, of course, evaluates to the expected [2, 2, 2]

map(lambda f: f(1), [lambda x: x + 1 for i in range(3)])

What do you think this evaluates to: also [1, 2, 3] or [3, 3, 3]
as before?

It's [2, 2, 2] !

-

You are a blatant troll and now a public moron to boot. Will you please
go away? You may consider coming back when you learn 1+1=2.

Ville Vainio

unread,
Oct 27, 2003, 1:48:13 AM10/27/03
to
mik...@ziplip.com writes:

> I present to you the final and ultimate proof of Python's
> brain-damage:

-1, Troll.

> map(lambda f: f(1), [lambda x: x + 1 for i in range(3)])

> Guess again, it's [2, 2, 2] !

Yes, 1+1 == 2 for usual values of 1.

> Pythonista, you are all very welcome to learn Haskell.

..

> All this Python bashing is starting to feel like mocking
> a retarded child...

Well, the Haskell community seems like a friendly bunch, at least.

--
Ville Vainio http://www.students.tut.fi/~vainio24

Isaac To

unread,
Oct 27, 2003, 1:50:18 AM10/27/03
to
>>>>> "mike420" == mike420 <mik...@ziplip.com> writes:

mike420> As you might have heard, the ever so generous GvR allowed us
mike420> write the same in Python (literal translation, really):

mike420> map(lambda f: f(1), [lambda x: x + 1 for i in range(3)])

mike420> What do you think this evaluates to: also [1, 2, 3] or [3, 3,
mike420> 3] as before?

mike420> Guess again, it's [2, 2, 2] !

Did you really mean

map(lambda f: f(1), [lambda x: x+i for i in range(3)])

? If so, it does return [3, 3, 3]. The only problem here is that Python
variables are name lookups over dictionaries, and as such the value of "i"
is not coined into the lambda. Rather it is "external", binded dynamically.
I believe the behaviour can be fixed rather easily by some directives, say
making it

map(lambda f: f(1), [lambda x: x+(*i) for i in range(3)])

where the * construct would get the reference of i at function definition
time rather than at function invocation time (anyone can point me to an
PEP?). On the other hand, I basically never use lambda this way.

mike420> Pythonista, you are all very welcome to learn Haskell. You
mike420> will find the syntax very familiar. Haskell is short too
mike420> (compare one line above that gives the correct result to
mike420> several Python lines that surprise you)

Yes, tried. Failed, unluckily. It is clear that I'm not the kind of people
who will map every programming problem into a mathematical problem and solve
it that way. So I hate maps and reduces. I hate that I cannot assign a new
value to an existing value to change its binding. I hate having to remember
the unintuitive meanings of all the symbols. I hate having to guess when
variables will get its bindings and when functions will be invoked during
lazy evaluation. There is no way that anyone can turn me to Haskell.
Basically every strong point Haskellers talk about turn me off. This is
very unlucky.

Regards,
Isaac.

Dirk Thierbach

unread,
Oct 27, 2003, 6:18:09 AM10/27/03
to
Ville Vainio <ville.spamm...@spamtut.fi> wrote:
> mik...@ziplip.com writes:

>> I present to you the final and ultimate proof of Python's
>> brain-damage:

> -1, Troll.
[...]


> Well, the Haskell community seems like a friendly bunch, at least.

You're right, he's a troll. As a user of Haskell, I think such postings
do an inexcusable amount of damage. Please ignore him, and don't
judge the Haskell community by people like him.

- Dirk

[F'up to poster]

Alex Martelli

unread,
Oct 27, 2003, 6:14:52 AM10/27/03
to
Isaac To wrote:
...

> Did you really mean
>
> map(lambda f: f(1), [lambda x: x+i for i in range(3)])
>
> ? If so, it does return [3, 3, 3]. The only problem here is that Python
> variables are name lookups over dictionaries, and as such the value of "i"
> is not coined into the lambda. Rather it is "external", binded
> dynamically. I believe the behaviour can be fixed rather easily by some
> directives, say making it
>
> map(lambda f: f(1), [lambda x: x+(*i) for i in range(3)])
>
> where the * construct would get the reference of i at function definition
> time rather than at function invocation time (anyone can point me to an
> PEP?). On the other hand, I basically never use lambda this way.

No new "directive" needed: it's easy to get "snap-shots" of current
values of names into a lambda or other function:

map(lambda f: f(1), [lambda x, i=i: x+i for i in range(3)])

The "i=i" gets the (outer) value of i at function-definition time and
injects it as local variable i in the lambda. You can also rename just
as easily (and sometimes it's clearer):

map(lambda f: f(1), [lambda x, k=i: x+k for i in range(3)])

Alex

Jeremy Fincher

unread,
Oct 27, 2003, 9:25:39 AM10/27/03
to
mik...@ziplip.com wrote in message news:<D5ODMGKEL3JQLZOFIJAG...@ziplip.com>...

> Ladies and Gentlemen,
>
> I present to you the final and ultimate proof of Python's
> brain-damage:
>
> As you may remember,
>
> flist = []
>
> for i in range(3)
> f = lambda x: x + i
> flist.append(f)
>
> [f(1) for f in flist]
>
> produces [3, 3, 3] in Python.
>
> In Haskell, we would express this as follows:
>
> map (\f -> f 1) [\x -> x + i | i <- [0..2]]

First, let's remove the list comprehension syntactical sugar. This is
actually:

map (\f -> f 1) (map (\i -> (\x -> x + i)) [0..2])

So, as you can see clearly without the sugar, you're basically
creating closures, since a new lexical scope is introduced at function
creation.

> This, of course, evaluates to the expected [1, 2, 3]

Expected by functional programmers who expect new lexical scopes to be
created at every block, sure. But Python isn't a functional
programming language, and doesn't create a new lexical scope in every
block.



> As you might have heard, the ever so generous GvR allowed us write
> the same in Python (literal translation, really):
>
> map(lambda f: f(1), [lambda x: x + 1 for i in range(3)

Others have pointed out your typo here; I'll assume you meant "lambda
x: x + i" instead of 1.

Again, let's remove the syntactical sugar here:

L = []
for i in range(3):
L.append(lambda x: x + i)
map(lambda f: f(1), L)

The observed result, [3, 3, 3] is perfectly in keeping with the fact
that Python does not create a new lexical scope in for suites (or if
suites, or any other non-class, non-function suite).

> Pythonista, you are all very welcome to learn Haskell.
> You will find the syntax very familiar. Haskell is short
> too (compare one line above that gives the correct
> result to several Python lines that surprise you)

The Python result is only surprising to those who try to impose other
languages' semantics on Python.

> All this Python bashing is starting to feel like mocking
> a retarded child...

You, sir, are a poor representative of the Haskell community.

Jeremy

Gonçalo Rodrigues

unread,
Oct 27, 2003, 10:28:24 AM10/27/03
to
On Sun, 26 Oct 2003 22:25:41 -0800 (PST), mik...@ziplip.com wrote:

>Ladies and Gentlemen,
>
>I present to you the final and ultimate proof of Python's
>brain-damage:
>

[text snipped]

>
>ROTFLMFAO!
>
>Pythonista, you are all very welcome to learn Haskell.
>You will find the syntax very familiar. Haskell is short
>too (compare one line above that gives the correct
>result to several Python lines that surprise you)
>
>All this Python bashing is starting to feel like mocking
>a retarded child...
>
>420

Trolls are not welcomed, get lost.

G. Rodrigues

Bengt Richter

unread,
Oct 27, 2003, 11:29:49 AM10/27/03
to

I think it would be nice if we could inject a value as a local variable
without being part of the calling signature for the function. E.g., if
the parameter list had a way to delimit "injected" values so that they
weren't part of the parameter list per se. E.g., using '|' to separate
local-variable presets,

map(lambda f: f(1), [lambda x|k=i: x+k for i in range(3)])

or in a more usual definition

def foo(x, y, z=0, **kw | dx=100, dy=200, dz=-40):
return x+dx, y+dy, z+dz

or formatted a little differently

def foo(x, y, z=0, **kw |
dx=100,
dy=200,
dz=-40
):
return x+dx, y+dy, z+dz

The semantics would be like ordinary default-value parameters, but
they would not be overridable with actual parameters, which if supplied
would raise the usual exception from too many args.

Regards,
Bengt Richter

Matthew Danish

unread,
Oct 27, 2003, 11:36:40 AM10/27/03
to
On Mon, Oct 27, 2003 at 04:29:49PM +0000, Bengt Richter wrote:
> I think it would be nice if we could inject a value as a local variable
> without being part of the calling signature for the function. E.g., if
> the parameter list had a way to delimit "injected" values so that they
> weren't part of the parameter list per se. E.g., using '|' to separate
> local-variable presets,

I think we've found a fan of &aux. =)


(defun foo (x y &optional (z 0) &aux (dx 100) (dy 200) (dz -40))
(values (+ x dx) (+ y dy) (+ z dz)))

--
; Matthew Danish <mda...@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."

Marco Antoniotti

unread,
Oct 27, 2003, 1:20:47 PM10/27/03
to

Bengt Richter wrote:
> On Mon, 27 Oct 2003 11:14:52 GMT, Alex Martelli <al...@aleax.it> wrote:
>

...

>>No new "directive" needed: it's easy to get "snap-shots" of current
>>values of names into a lambda or other function:
>>
>>map(lambda f: f(1), [lambda x, i=i: x+i for i in range(3)])
>>
>>The "i=i" gets the (outer) value of i at function-definition time and
>>injects it as local variable i in the lambda. You can also rename just
>>as easily (and sometimes it's clearer):
>>
>>map(lambda f: f(1), [lambda x, k=i: x+k for i in range(3)])
>>
>
>
> I think it would be nice if we could inject a value as a local variable
> without being part of the calling signature for the function. E.g., if
> the parameter list had a way to delimit "injected" values so that they
> weren't part of the parameter list per se. E.g., using '|' to separate
> local-variable presets,
>
> map(lambda f: f(1), [lambda x|k=i: x+k for i in range(3)])

Well then, AFAIU, the

lambda x, k = 1: ...

idiom serves to introduce an optional lambda bound variable.
I.e. something that in CL has been there since 1984 and earlier. Your
suggestion is instead along the lines of the &AUX lambda parameter
already present in Common Lisp (another non surprise) since 1984 and before.

But this begs the question. The bottom line is that Python needs to get
proper lambda expressions. The extra "quote" local "unquote" argument
is not what you'd expect.

Cheers
--
Marco Antoniotti

Matthias Felleisen

unread,
Oct 27, 2003, 9:37:49 PM10/27/03
to
Jeremy Fincher wrote:

> Expected by functional programmers who expect new lexical scopes to be
> created at every block, sure. But Python isn't a functional
> programming language, and doesn't create a new lexical scope in every
> block.
>

> The Python result is only surprising to those who try to impose other
> languages' semantics on Python.

Yes, that's true but functional programmers come from a "carry your semantics
on your sleave" angle at things. Let's see how we build this in Scheme. If I
understand Python's semantics properly, a for-iterator has this semantics:

(define-syntax python-for
(syntax-rules (in range)
[(_ x in (range n) exp ...)
(let ([x #f])
(for-each (lambda (i) (set! x i) exp ...) (build-list n identity)))]))

It sets up an iteration variable and then mutates the iteration variable for
each iteration. Then you evaluate the body of the for. A Schemer wouldn't cut
the overhead and write/expect something like that:

(define-syntax scheme-for
(syntax-rules (in range)
[(_ x in (range n) exp ...)
(for-each (lambda (x) exp ...) (build-list n identity))]))

Here we just iterate over the specified variable. (I have the iteration order of
the number sequence backwards. That's intended to skip a small bit of syntactic
overhead.)

Now depending on which of these for's you use you get different results for

(define flist '())

(define f #f)

(for i in (range 3)
(set! f (lambda (x) (+ x i)))
(set! flist (cons f flist)))

(map (lambda (f) (f 1)) flist)

Nothing surprising here for someone who studies language semantics. Things are
as they are. But to someone who compares these things, I must admit that I am
astonished that Python chose the first, complicated (not on your sleave)
semantics and rejected the second one.

-- Matthias

Alan Offer

unread,
Oct 30, 2003, 10:01:06 AM10/30/03
to
Isaac To <kk...@csis.hku.hk> wrote in message news:<7ioew3x...@enark.csis.hku.hk>...

> I believe the behaviour can be fixed rather easily by some directives, say
> making it
>
> map(lambda f: f(1), [lambda x: x+(*i) for i in range(3)])
>
> where the * construct would get the reference of i at function definition
> time rather than at function invocation time (anyone can point me to an
> PEP?).

We can essentially do this by passing i to a function that then returns the
desired function. So to make flist with the functions that mike420 wanted,
we can use:

flist = map(lambda i: (lambda x: x+i), range(3))

Now [f(1) for f in flist] is [1, 2, 3] as he expected.

Marco Antoniotti

unread,
Oct 30, 2003, 4:52:34 PM10/30/03
to

Alan Offer wrote:

This is good and I stand corrected then. At least you can achieve this
with Python.

Cheers
--
marco

Isaac To

unread,
Oct 30, 2003, 9:06:53 PM10/30/03
to
>>>>> "Alan" == Alan Offer <alan_...@hotmail.com> writes:

Alan> Isaac To <kk...@csis.hku.hk> wrote in message
Alan> news:<7ioew3x...@enark.csis.hku.hk>...


>> I believe the behaviour can be fixed rather easily by some
>> directives, say making it
>>
>> map(lambda f: f(1), [lambda x: x+(*i) for i in range(3)])
>>
>> where the * construct would get the reference of i at function
>> definition time rather than at function invocation time (anyone can
>> point me to an PEP?).

Alan> We can essentially do this by passing i to a function that then
Alan> returns the desired function. So to make flist with the functions
Alan> that mike420 wanted, we can use:

Alan> flist = map(lambda i: (lambda x: x+i), range(3))

Alan> Now [f(1) for f in flist] is [1, 2, 3] as he expected.

Ah... that means

map(lambda f: f(1), [(lambda i: lambda x: x+i)(i) for i in range(3)])

Seems make sense to me.

Regards,
Isaac.

0 new messages