I am new to web2py and python both. I am writing a sample blog app in this framework. I want to split the business logic that gets called in each controller method to it's own module, and found this example helpful:
- http://www.web2pyslices.com/slice/show/1478/using-modules-in-web2py
- Cleaning up web2py my controllers
As you can see, you need to import objects in modules or set them through globals.current. The controller can refer to "db" and "request" instances (for example) without any import. What kind of coding mechanism makes it possible in controller but not elesewhere?
Found the answer: Looks like how web2py works is by compiling the python code for the controllers and models and views on the fly. It runs them in it's special 'environment' Related snippets of code are:
https://github.com/web2py/web2py/blob/master/gluon/main.py#L205-263 In the file above, look at: build_environment, run_models_in, run_controller_in functions (below):
Which run the python code in a 'restricted' environment: