what's wrong with "lambda x : print x/60,x%60"

Dan Sommers me at privacy.net
Wed Dec 7 08:09:00 EST 2005


On 07 Dec 2005 03:25:53 -0800,
Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> Steve Holden <steve at holdenweb.com> writes:

>> > All joking aside, when I have names (temporary variables or
>> > scaffolding functions) that I need to initialise a module or data
>> > structure, but then outlive their usefulness, I del the name
>> > afterwards. Am I the only one? I can't say I've seen anyone else
>> > doing that, and it feels icky to me (for no reason I can put my
>> > finger on) -- what do others think?

> I do that too sometimes.  I think it's a Python weakness that you
> can't declare a local var like in other languages, to go out of scope
> at the end of the current block, e.g.:

>   if cond:
>     my x = 7    # make a new scope for x, goes out of scope at end of if

For a dissenting opinion:  I *like* Python's three-scope setup.  The
last thing I want is to get into an if statement (or two) inside a for
statement inside a method definition inside a class definition and to
have to wonder if "x" is local to the if-suite (or the outer if-suite),
the for-suite, the method definition, the class definition, or the
module.  And allowing multiple "x"'s in all these different scopes to
shadow each other just makes it worse.

IMO, if my suites, methods, classes, and modules are sufficiently large,
and their documentation and my text editor sufficiently weak, that I
can't figure out a "good" local name to use at any given point in the
code, or that I have to wonder which "x" I really want at that point, or
that I have to worry about creating a sub-suite (with no extra scoping)
and clobbering an important name, then I've already done something
enormously wrong.

> Well, that makes the code a little bit confusing.  If you say

>    x = some_intermediate_result(...)
>    do_something_with (x)

> do you know for sure whether x will be needed again?  Are you sure you
> didn't use the name x further up in the function for something that's
> still needed?

I never know for sure whether x will be needed again.  But if the
function from which you took that snippet is large enough that there's
any question about it, then perhaps it's too large already.  And if
there's a chance that x might be useful elsewhere, then I should have
called it something other than x (or immediately rename it as soon as I
start to think about using its contents again).

(Okay, now I sound like a curmudgeon, but I'm not.  At least I don't
think I am.  I'm just trying to present an alternate view, one in which
the issue of needing more scoping controls isn't an issue.)

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list