In C:
#include "foo.h"
int main()
{
}
I believe that “foo.h” effectively gets copied and pasted in at the spot of the “#include”.
Python imports are different though, I’m finding.
I just refactored a bit of GAE code that initially had ALL request handlers in one big index.py file.
NEW directory tree:
+ | +- [handlers] // all inherit webapp.RequestHandler +- [models] // all inherit db.Model | +- globals.py // contains global variables for site-wide settings +- index.py // contains all handler redirects
[handlers] is the folder with the handlers
[models] is the folder with the models
So, index.py goes
from globals import * # we need all of the globals
# ...
from handlers.FirstPage import FirstPage
from handlers.SecondPage import SecondPage
#.. etc.
SHOULDN’T handlers.FirstPage and handlers.SecondPage “see” everything in globals, since globals is imported “first”, before handlers.*?
While in C it works more or less “copy-pasting” the code, in Python is quite different.
Remember the Zen of Python?
Each time you import a module, you execute its code, but you keep all the scopes of the definitions. So, when you import
handlersyou giveindexaccess to the scope ofglobals, but thehandlersmodule has no access toglobalsmodule unless you explicitly allow it to access to the scope, importing it.