Main Content

Sample code with errors in it on our web site

Archive - Originally posted on "The Horse's Mouth" - 2009-10-29 05:53:31 - Graham Ellis

Feedback is lifeblood ... ignore it at your peril, and remember that for each person who lets you know you have a problem, a further ten will have noticed and not said anything. From my mailbox:

> Your webpage
>
> http://www.wellho.net/resources/ex.php4?item=y105/locvar.py
>
> contains the following code ...
>
> [snip]
>
> which is erroneous.

I totally agree. It does generate an error in the last line. Why?

The immediate answer is that variables used within defined functions / methods in Python are local to those functions, and unless returned or declared global are NOT available outside in the calling code. When you think about it for a minute of two, this is an excellent default behavior as it means that you don't clutter up your main variable name space with internal names from other piece of code you have packaged into self-contained units. If your language uses "default global" which means that variables are shared unless you declare otherwise, you can end up with the most enormous problems when you come to build an application using code blocks (functions / methods / objects) from several sources and they have a variable name conflict. Perl's use strict pragma is an example of a language that is "default global" but has an addition to let you circumvent the problems it causes.

There is a second very different answer too if you are asking why we have erroneous code on our site. Many of the examples on the site come from our training courses, and were initially uploaded as a service to our delegates - so that they could try out the code after the course without the need to retype it. Some of the examples intentionally show the sort of things that generate error messages, and the notes for the course go through the "why" and "wherefore" of that - indeed, on of the most important things you learn on a programming course is how to 'read' the error messages - interpreting what they mean - so of necessity there will be failing examples in the notes, thus on our site.

Jxx's feedback is doubly useful - it alerted me to the fact that a page put up for a very good reason has now become much more visible, and that the explanation that was offered further on in the page:
>>> Remember that some of our examples show you how not to do things - check in your notes.
was far too well buried, and generic.

Rather than change an example which is there for good cause, and forms part of our Python Programming and learning to program in Python courses, I have added some comments to say that it shows how an error is generated, and why. Always good to comment your code ;-) and hopefully much clearer now. It will, though, need to be revised further in the near future to take accocunt of Python 3 ;-)

Jxx continued:

> I'm not trying to be a smart-ass since python is certainly not
> my best language but you really shouldn't have erroneous code
> advertising your expertise.

Totally agree (with the conclusion - he probably knows a lot more Python that he lets on!) - and "Thank you" letter sent. The page, if you would care to click here still illustrates the error, but now clearly explains why the code doesn't work. Which is what you want to learn on a training course.