[Edu-sig] Kirby Urner's Sieve of Eratosthenes

Ka-Ping Yee pingster@ilm.com
Mon, 5 Jun 2000 17:35:07 -0700 (PDT)


On Mon, 5 Jun 2000, Kirby Urner wrote:
> 
> Here's an ugly way to avoid lambda -- illustrative of
> points made in 'Learning Python: Nested Functions
> Aren't Nested Scopes' (pg 118-120):
[...]
>         global sieve  # force to module level

This is the key line.

>         def final(x): return sieve[x]  # internal function

Switching "lambda" to "def" doesn't really make much difference.
Or in other words, it's not lambda's "fault".

"lambda" is just a shorthand for "def"..."return".  The two lines:

    final = lambda x: sieve[x]

    def final(x): return sieve[x]

are entirely equivalent.

It's making "sieve" a global variable that really makes
the difference here (naturally, having the function poke
around in its module space is generally not a good idea).


-- ?!ng