Is it me or a python wart

John Roth johnroth at ameritech.net
Tue Apr 30 07:30:41 EDT 2002


"Robin Becker" <robin at jessikat.fsnet.co.uk> wrote in message
news:Io2j9JAgacz8EwST at jessikat.fsnet.co.uk...

> the one thing I think is that other languages eg pascal/modula have a
> better/simpler grasp of what humans see in programs. Lexical scope ie
> higher on the page and further left means precede is a 'good' rule.
The
> 'local' idea that anything that is assigned to is 'local' is much
harder
> since that can be realised very distantly. I assume there are reasons
> for the current 'oddness', but they seem to be hidden from us poor
> lusers.
> --
> Robin Becker

If you do that, you can get some very non-intuitive behavior.

For example:

c = 2
def foobar(a):
    b = c
    c = a

In Python, the first assignment fails because 'c' is an
undefined local variable. I've worked in languages
where 'c' would be resolved as 2, and then suddenly
change in the middle of the function to be resolved
as the parameter 'a'. It's a very fertile source of bugs.

Consider this loop:

a = 2
for x in foobar:
    c = a
    a = x

The first time through the loop, 'c' is set to a
value global to the loop, the rest of the time,
it's set to the loop variable (on the preceding pass!)
This is also very non-intuitive behavior.

However, I suspect the primary motivation for this
is optimization. The variable 'dictionary' in functions
is not a dictionary, but a structure that allows faster
access. Given that, the 'if it's assigned to, its a local
variable' rule is quite reasonable.

In any case, any function or method that's big
enough to cause a problem is probably too big.
The 'one page rule' was in existence thirty years
ago when we had to look at programs on paper!

John Roth





More information about the Python-list mailing list