PyWart: NameError trackbacks are superfluous

Rick Johnson rantingrickjohnson at gmail.com
Sat Mar 16 20:11:48 EDT 2013


On Saturday, March 16, 2013 6:48:01 PM UTC-5, Steven D'Aprano wrote:
> On Sat, 16 Mar 2013 21:19:34 +0000, Oscar Benjamin wrote:
> > [...]
> > NameErrors can occur conditionally depending on e.g. the
> > arguments to a function. Consider the following script:
> [...]
>
> Correct, although in your example, simply pointing at the
> relevant line of code is enough to establish the error.

EXACTLY!

> [...]
> Here's a truly trivial case where code will succeed or
> fail depending on the order of function calls.
>
> def display():
>     print("spam = %d" % spam)
>
> def start():
>     global spam
>     spam = 23
>
> def stop():
>     global spam
>     del spam
>
> def run():
>     print("*** Succeeds ***")
>     start()
>     display()
>     stop()
>
> def fail():
>     print("*** Fails ***")
>     start()
>     stop()
>     display()
>
> run()
>
> fail()
>
> It's not enough to know that the print line in display()
> fails, because that's merely the side-effect. The actual
> problem occurs in the caller, fail().

No, the "ACTUAL PROBLEM" is in the author.

Who would be stupid enough to write code that depends on globals that *may* or *may not* exist, and then go an add insult to injury by not testing for the name before executing the code? Your example is a fine example of why using globals is foolish.

Congratulations Steven, you drove the car into the ditch -- even a noob can do that!




More information about the Python-list mailing list