problem with variable scoping
Daniel E. Wilson
chronos at teleport.com
Wed May 26 16:02:27 EDT 1999
Michael Dingler wrote:
> It's...
>
> an embarassing question. Yes folks, I'm writing my first
> Python program (a text to HTML converter) and I have a
> slight misunderstanding with the variable scoping...
>
> Let me present some code fragments:
>
> last_line_empty = 0
> .
> .
> .
> def process(line):
> ...
> if last_line_empty:
> pass
> else:
> print "<P>"
> last_line_empty = 1
Your thinking like this is a C or Pascal program. The assignment of
last_line_empty creates an instance in the local scope. It does not
modify the instance in the enclosing scope.
Add 'global last_line_empty' at the top of the function and the problem
should be fixed.
--
Daniel E. Wilson
The idea that Bill Gates has appeared like a knight in shining armour to lead
all customers out of a mire of technological chaos neatly ignores the fact
that it was he who, by peddling second-rate technology, led them into it in
the first place.
- Douglas Adams
More information about the Python-list
mailing list