On May 4, 2010, at 4:26 AM, canna wrote:

> Thank you for the answer
> so how would you share functions between controllers easily? without
> passing global objects to the functions all the time?
> is the only way is to bundle it all in one class that will be
> initialized with the 'db' variable?
> or there is other way?

You can put shared functions (or classes) in a model file instead. I use 
modules for decoupled, reusable code, model files for coupled app-specfic 
shared code. You just have to get past the idea that just because they're 
called 'models' doesn't mean that's all they can contain.

> 
> 
> 
> 
> On May 4, 1:49 pm, mdipierro <[email protected]> wrote:
>> A module is a module. You can import stuff form the module but the
>> module will not see variables defined in the calling environment.
>> In the module you can defined a function that takes db as parameter,
>> import the funciton and call it with the db instance. You will have
>> the problem not just for db, but also for the other web2py global
>> objects.
>> 
>> Massimo
>> 
>> On May 4, 4:16 am, canna <[email protected]> wrote:
>> 
>>> hi all,
>>> I want to share a bunch of functions between controllers, so I moved
>>> them to a module called 'utilities' in the modules folder, and in the
>>> controller I import the module as suggested here in the forum:
>> 
>>> import applications.myapp.modules.utilities as util
>>> reload(util)
>> 
>>> the problem is, it's not working, I get an error:
>> 
>>> NameError: global name 'db' is not defined
>> 
>>> how can I get my 'utilities' file to recognize the global variable
>>> 'db'?


Reply via email to