AaronL schrieb:
> I was running through the creating a form with ToscaWidgets tutorial
> for TG2, and when I got to the validate part of the tutorial, I kept
> on running into an error with the @validate line. The @validate line
> is the following:
>
> @validate(create_movie_form, error_handler=new_movie)
>
> However, it complained that it couldn't find create_movie_form, even
> through create_movie_form was declared in the same file (root.py) as
> described in the tutorial:
>
> create_movie_form = MovieForm("create_movie_form",
> action='submit_movie', submit_text = 'Add movie')
>
> I thought that maybe it was an ordering thing, so I moved the
> declaration of create_movie_form to earlier in the file, before the
> use of @validate, and this resolved the problems with @validate.
>
> In general, order doesn't really matter much in Python source code
> with respect to class declarations and the like, but it does seem to
> make a difference with the @validate decorator. Is this the expected
> behavior for a decorator like @validate, or should it be considered a
> bug?
Would you expect the following to work?
def f(a, b):
return a * b
f(x, y)
x = 10
y = 20
Certainly not. So why do you expect it to work with a decorator? A
decorator is an expression that must yield a callable.
Make a simple test:
def mydecorator(f):
print "I'm decorating"
yield f
@mydecorator
def foo():
pass
As you will see, the decorator is executed when foo is created - no need
to invoke foo!
Now this is more like what validate is:
def my_decorator_function(message):
def the_real_decorator(f):
print "I'm decorating with", message
return f
print "I'm producing a decorator"
return the_real_decorator
@my_decorator_function("a message")
def foo():
pass
again, notice the order of messages appearing.
Summary: validate is a *function* returning a decorator.
Function-arguments must be known.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---