Is it me or a python wart

Robin Becker robin at jessikat.fsnet.co.uk
Tue Apr 30 14:36:13 EDT 2002


In article <uct01lso59lu7d at news.supernews.com>, John Roth
<johnroth at ameritech.net> writes
>
......
>> 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.
>

in a lexical language the first ref(a) is the global, later ones are the
local, but I suppose any such rule is breakable/variable. I suppose
the static rule is easier to check statically, but here we seem
to have a mix of static and dynamic. 


>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.
>

That is the root of the problem

>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
>
>
No matter how small the fact that we have to look down & right to check
is a problem. People make assumptions about the way things work. Of
course we shouldn't and get bitten time and time again. If the rules
didn't have so many teeth we'd be better off.
-- 
Robin Becker



More information about the Python-list mailing list