Pattern to get rid of imports in modules as in web2py controllers

148 Views Asked by At

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:

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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:

1
On

The web2py framework does a lot of behind the scenes work to make all that stuff available. For example, when you go to a URL like host/app/controller, that controller is called by web2py (starting with something in web2py.py) that handles importing web2py modules, providing request/response objects, etc.

Things placed in modules, however, are intended to be standalone Python code, not necessarily specific to web2py.