#1
|
|||
|
|||
Modules / Require / Include function
Developing different programs that use some of the same code I was wondering if python also has a fuction like e.g. php to include small code snippets at certain positions.
I've already used the module-idea (e.g. import graphModule and then using it with graphModule.blabla ) but it is not the same as just outsourcing part of the code. Is there some command to do this? Johannes |
#2
|
|||
|
|||
Hi,
Python doesn't have a built-in way of doing this. You pretty much have to use the import syntax. You could try the following: Code:
execfile('graphModule.py') |
#3
|
|||
|
|||
Thank you for the fast answer.
Yes, execfile('graphModule.py') works. Why would you not recommend it? Is the file called loaded from the harddrive every time I call execFile(File)? => slow down Johannes |
#4
|
|||
|
|||
Hi,
I believe every time you call execfile, python will look on the hard drive for the filename. I think Python optimizes this a little by compiling the script into a .pyc file, and using that on subsequent calls. I'm not sure if Python stores the code in memory for further optimizations. There really is no harm in doing this, I just see it as a bad workaround to simply importing the module. |
#5
|
|||
|
|||
OK, thank you.
Johannes |
|
|