import namespace issues (why do I want this)

Harald Kirsch kirschh at lionbioscience.com
Tue Jul 10 07:35:40 EDT 2001


Harald Kirsch <kirschh at lionbioscience.com> writes:

> THE PROBLEM:
> I have to separate a module's function into two .py-files. Now I would
> like to import both in a way that they behave like they are one file.


Several people asked me to describe what I really want to do. Chances
are there is a cleaner solution. However it may be preempted due to
historic code I wrote when I started to use Python and which I cannot
change easily.

One file, the historic one (call it littlelanguage.py), contains
functions which are intended to be used by more-or-less
non-programmers as a little language. They are not expected to know
Python. They only know the basic syntax and those
functions. Consequently they write code which does not contain any
`import' and the functions are not qualified.

Those functions don't do a lot. Usually they soon call more basic
functions which do the real work. To allow for more than one
implementation of the basic functions, they go into other files, say
basicimpl1.py, basicimpl2.py, etc.

When calling the basic functions, those functions in littlelanguage.py
don't qualify them (which is bad, I know now). Consequently the
implementation functions must be in the same globals()-context as the
functions in littlelanguage.py. Because I have more than one
implementation, I cannot put

  from basicimpl1 import *

into littlelanguage.py. In fact I cannot change this file at all.


The solution I came up with for now is to put the following code into
every basicimpl*.py:

  import imp
  exec imp.find_module("littlelanguage")[0]
  del imp

However, it does not compile littlelanguage.py and even breaks if it
finds a compiled version.

Suggestions welcome and thanks for the replies I found already.

  Harald Kirsch

-- 
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***



More information about the Python-list mailing list