Elementary string-parsing

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Feb 4 11:30:21 EST 2008


On Mon, 04 Feb 2008 12:25:24 +0000, Odysseus wrote:

> I'm not clear on what makes an object global, other than appearing as an 
> operand of a "global" statement, which I don't use anywhere. But "na" is 
> assigned its value in the program body, not within any function: does 
> that make it global?

Yes.  The term "global" usually means "module global" in Python.

> Why is this not recommended?

Because the functions depend on some magic data coming from "nowhere" and
it's much harder to follow the data flow in a program.  If you work with
globals you can't be sure what the following will print:

def spam():
    global x
    x = 42
    beep()
    print x

`beep()` might change `x` or any function called by `beep()` and so on. 

Another issue is testing.  If you rely on global names it's harder to test
individual functions.  If I want to test your `extract_data()` I first have
to look through the whole function body and search all the global
references and bind those names to values before I can call the function. 
This might not be enough, any function called by `extract_data()` might
need some global assignments too.  This way you'll get quite soon to a
point where the single parts of a program can't be tested in isolation and
are not reusable for other programs.

In programs without such global names you see quite clearly in the
``def`` line what the function expects as input.

> If I wrap the assignment in a function, making "na" a local variable, how
> can "extract_data" then access it?

Give it as an argument.  As a rule of thumb values should enter a function
as arguments and leave it as return values.

It's easy to "enforce" if you have minimal code on the module level.  The
usual idiom is:

def main():
    # Main program comes here.

if __name__ == '__main__':
    main()

Then main is called when the script is called as program, but not called if
you just import the script as module.  For example to test functions or to
reuse the code from other scripts.

>> def extract_data(names, na, cells):
>> 
>> 	and
>> 
>> 	return <something>
> 
> What should it return? A Boolean indicating success or failure? All the
> data I want should all have been stored in the "found" dictionary by the
> time the function finishes traversing the list of names.

Then create the `found` dictionary in that function and return it at the
end.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list