scope of module functions

Frank Sonnenburg sonnenbu at informatik.uni-bonn.de
Sat Jun 8 19:58:43 CEST 2002


"Chris Liechti" <cliechti at gmx.net> schrieb im Newsbeitrag
news:Xns9227C812CD4B2cliechtigmxnet at 62.2.16.82...
> "Frank Sonnenburg" <sonnenbu at informatik.uni-bonn.de> wrote in
> news:adte4c$nu8$1 at f1node01.rhrz.uni-bonn.de:
> > a little tricky problem (at least from my point of view); given module
> > CALLER:
> >
> > # i need "fct" and some other from module "UTILS"
> > from UTILS import *
> > fct()
> > # END of module CALLER
> >
> >
> > Now, inside fct(), how do i determine the module - in this case CALLER -
> > from where fct() was called?
>
> i think __name__ can help in this case but not in the one below.
> but are you sure that you wan't t do this? a function that behaves
> differently depending on the namespace gives very hard times debugging...
>
> a cleaner solution would be to create an class that contains fct and each
> module that uses it can create its own object.
>
> if you can say what you want to achieve, we might give you a more specifuc
> advice.
>
> > Even concerning cases like (continuation of example):
> >
> > # Now we are in __main__
> > import CALLER
> > fct_in_main = CALLER.fct
> > fct_in_main()
>
>
> --
> Chris <cliechti at gmx.net>
>

Yes, actually i thought of classes as a workaround.

I have a tool with a large amount of functions, so i grouped them into a
menu tree. My first approach was a package solution, which has nice
advantages: easy to build, unique access to any function. But there is a
main disadvantage: function names can get very long, for instance:
utils.database.sql.fetch()

Instead i want to enter:
>> import utils
>> database()
# do something in database, then switch to submenu sql
>> sql()
# now sql commands like fetch() are visible
>> val = fetch('name')
# now return to parent menu database
>> end()
# now fetch() is not available any more
.........


Anyway, thanx a lot for your quick response!

    Frank





More information about the Python-list mailing list