cjparson:

I think 2 things will help this make more sense to you:

Try this outside of web2py, something like this (I used ipython):
/------------------------------------
In [16]: def dec(f):
   ....:     print("dec called")
   ....:     return f
   ....:

In [17]: def i1():
   ....:     print ("in i1")
   ....:

In [18]: @dec
   ....: def i2():
   ....:     print("in i2")
   ....:
dec called

In [19]: i2
Out[19]: <function i2 at 0x014976B0>

In [20]: i2()
in i2

In [21]: i1()
in i1

In [22]:
\-------------------------

If you look at gluon/main.py::wsgibase()   you'll start to get insight into
what web2py is doing:

- when a request for a page comes in, wsgibase() parses it and tries to
determine is it's valid, and if so, which controller should be called.

- once all is ok, and your "index()" is the target function, then
serve_controller()  (also in main.py) is called.   This is how a page is
generated.   And environment is setup for the session, a view is selected,
and THEN models are run, and THEN controllers...

So - what are you seeing?

You are seeing your contollers file being run at each page request (reason
to keep controller files rather compact).   Each time a definition of
index2() happens, you see your 'print("decorator called")'

When a page request for index() happens, the controller (default.py) is
found, and a function within that controller (will be) called - once the
file is loaded.

Loading the file yourapp/controllers/default.py  results in the print
statement....

Which leads to your second "Aha" (about to come):   You probably did not
want a decorator on the definition of a function - you would usually use a
decorator before a function CALL (thus decorating the CALL).

But you probably could not figure out how to accomplish this in your
controller, since you are only defining the function ---
gluon/main.py::serve_controller()::run_controller_in()   is making the
function call...

So, to get to that second "Aha" - how do you decorate a function you want to
run as a request?

Indirectly (when you have execution control), like this:

def index():
   @decorator
   index2():

Then you will get more of what you are expecting.

Does that help?

If you'd like, I can post links to a couple of presentations on decorators
that I find useful.

Regards,
Yarko



On Fri, Dec 19, 2008 at 11:25 AM, mdipierro <[email protected]> wrote:

>
> that is how decorators work.
>
> @decorator
> def f(): return
>
> is the same as
>
> def f(): return
> f=decorator(f)
>
> so the decorator is called upon definitions, not when the function f
> is called. unless you make this more complex
>
> def decorator(f):
>    def _decorator():
>         print 'hello world'
>         f()
>    return _decorator
>
> Massimo
> On Dec 19, 2:30 am, cjparsons <[email protected]> wrote:
> > If I have a controller file as below I find that the decorator()
> > function is called when any controller in the file is called not just
> > the decorated ones. i.e. If I access /application/default/index I see
> > "decorator called" as well as when I access /application/default/
> > index2. Am I doing something stupid? Thanks, chris.
> >
> > def decorator(func):
> >     print("decorator called")
> >     return func
> >
> > def index():
> >     response.flash=T('Welcome to web2py')
> >     return dict(message=T('Hello World 1'))
> >
> > @decorator
> > def index2():
> >     response.flash=T('Welcome to web2py')
> >     return dict(message=T('Hello World 2'))
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to