[Tutor] executing a function in a different file and class

Lloyd Kvam pythonTutor at venix.com
Fri Aug 20 23:33:21 CEST 2004

On Fri, 2004-08-20 at 17:03, vicki at thepenguin.org wrote:
> To simplify, I have this:
> file A: function1, Class1 with other functions which call function1
> file B: function2 which calls function1 but can't see a variable which was
> declared as global inside Class1.

Unless one of the experts says otherwise, there is no overarching name
space (except builtins in some sense).  Each file represents a separate
name space.  Python will try to resolve names in your current name
space.  If you import a file, you can reference that name space
explicitly by using the file name.

Presumably file_A has a line that says:
import file_B
and file_B has a line that says
import file_A

(These are circular imports and can lead to grief.)

>From file_B you could write

I think this is what you are asking for.  The fact that these files are
so mutually dependent on each other suggests that you might want to
combine them into one file OR put the shared pieces into file_C so that
both A and B import file_C, but no longer import each other.  You would
still be able to reference



Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582

More information about the Tutor mailing list