is this a valid import sequence ?

Steven D'Aprano steve at
Mon Jun 25 13:46:02 CEST 2007

On Mon, 25 Jun 2007 05:08:00 +0000, Michele Simionato wrote:

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

Why do you do that? I'm not arguing that you shouldn't, but I'm trying to
understand your reasoning. Are you assuming (for the sake of the argument)
that there's a bug somewhere in the code? If you're trying to track down a
bug, you'll likely need to look at get_frommet regardless of the presence
or absence of the global statement. Or are you trying to analyze the
entire module? If so, you also have to dig into get_frommet.

(I repeat, I'm not saying you shouldn't, but I'm trying to understand why
you think the way you do.)

> and finally I would have reached the
> conclusion that
> 4. the author was completely wrong and used global without
>    knowing its meaning.

So you're with Alex that "redundant" == "wrong"?

I still can't my head around that. To me, redundant and wrong are
orthogonal, not synonyms. 

This code is wrong but not redundant (assuming you have a need for such
a function):

def sin_deg(x):
    """Return the sine of x degrees."""
    return math.sin(x/math.pi*180) # oops! should be x*math.pi/180

To me, this code is redundant but not wrong:

def sin(x):
    return math.sin(x)

It's not wrong, because it does everything that it is supposed to do, and
nothing that it isn't supposed to do. 

Am I wrong?


More information about the Python-list mailing list