[Tutor] does python have something like "#include" in C?
wescpy at gmail.com
Mon Jun 29 19:46:13 CEST 2009
> However, it seems that I can't put the function definition in a file
> and import it because I can't find a way to refer to an object in the
> main program file from within a module file. I understand that it's a
> good thing to contol which namespaces are referenced by which code but
> isn't there sometimes a need for code in a module to access the main
> program file's namespace? My example may be a little contrived but
> isn't this ability legitimately needed at times?
you are wrong and right. :-)
take your function definition and store in a file called, say
showmodule.py. then put the remaining code inside something like
foo.py. at the top of foo.py, you'll need:
from showmodule import show
then everything else will work. there is another syntax that is less
recommended, and that is, "from showmodule import *" -- with the
danger being exactly what you said above... you are "polluting" your
(global) namespace by bringing in *everything* from the other module
without much knowledge (necessarily) of what those names are, which
may potentially conflict with variables with the same names (if they
exist) in your local module.
hope this helps!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
More information about the Tutor