is this a valid import sequence ?
Michele Simionato
michele.simionato at gmail.com
Mon Jun 25 01:08:00 EDT 2007
On Jun 24, 1:29 pm, Steven D'Aprano
> I would like to hear your opinion of whether the
> following two functions are equally as wrong:
>
> def f1(gizmo):
> global spam # holds the frommet needed for the gizmo
> gizmo.get_frommet(spam)
>
> def f2(gizmo):
> # global spam holds the frommet needed for the gizmo
> gizmo.get_frommet(spam)
>
> I'm sure they're both wrong, but I'd like to know if there are degrees of
> wrongness.
I am not Alex Martelli, but I will tell you my opinion anyway.
To me f2 is not wrong: at worse you can say that the comment
is redundant since it is already clear from the code that
spam is a global, but it is not a big deal. As a code
reviewer I would not have had issues with f2. OTOH I would
have had serious issues with f1. Since the global
statement in correct Python code is solely used to declare
that a global variable is being set in an inner scope, I
would have to guess that:
1. function f1 wrong; maybe the author cut and pasted it
from someplace, forgetting the line where the global
variable spam was set;
2. maybe f1 is right, but then the author forgot to remove
the global declaration after the cut & paste;
3. the author does not know Python, and he believes that he
has to use global to denote the fact that the method
gizmo.get_frommet(spam) is setting a global variable.
So I would have had to look at get_frommet to see that actually
'spam' is not set there, and finally I would have reached the
conclusion that
4. the author was completely wrong and used global without
knowing its meaning.
All that analysis would have cost me some time, potentially
a lot of time depending on the complexity of the code, and
all that time would have been wasted time.
So f1 is misleading code, and I consider misleading code
actually *worse* than wrong code, since it makes you waste
your time without a good reason.
Michele Simionato
More information about the Python-list
mailing list