Help on scope/namespace

Byron Morgan lazypointer at yahoo.com
Sun Feb 2 14:35:55 EST 2003


Thanks, Paul, I guess I was not clear.

My script uses 10 dictionaries (2 of which contain > 500 elements) and 87
functions to parse randomly occurring strings. Loading the dictionaries from
a file works fine, and this helps a lot (the file is 53k, 1500 lines). The
functions I want to store in a file comprise another 1300 lines of code.

Because Python processes sequentially, functions can't be called until they
have been read in, so I can't put them at the bottom of the script as I
always have in REXX, where I have built most of my scripts for longer than I
care to admit.

Depending on the parsing results, globals are modified , so that other
functions can use them.

I find that if I load a function from a file, the dictionaries are not
available.

It is not practical to pass all 10 dictionaries as parameters  on each
function call, then pass them back as results.

It seems to me there should be a way to read a function in from a file and
have it operate as though it were in the main script.

But, perhaps there is not. Too bad.

Byron Morgan



"Paul Rubin" <phr-n2003b at NOSPAMnightsong.com> wrote in message
news:7xd6ma4n8y.fsf at ruckus.brouhaha.com...
> "Byron Morgan" <lazypointer at yahoo.com> writes:
> > Is there some way I can import functions from a file, and have them
behave
> > just as though they are contained in the main script ?
>
> Yes, just say "from file import function".  Or else prefix the function
> name with the module name.
>
> For example, you can start up Python and type:
>
>   import math
>   print math.sqrt(25)   # should print 5
>   print sqrt(25)        # throws an exception since sqrt is not in
namespace
>
> Alternatively, you can type:
>
>   from math import sqrt
>   print sqrt(25)        # this will work now






More information about the Python-list mailing list