On 26/04/2006, at 3:23, Roger Rohrbach wrote:
>
>> Widget's, for example, are one thing you'd like to reuse in many
>> requests, hence better not instantiate them unnecesarily.
>
> Sure. What I'm objecting to is that they are instantiated (if you're
> using the WidgetsDeclaration syntax) when the class is defined. I'm
> fairly new to Python, so maybe it's un-Pythonic to say it, but I don't
> think importing a file should execute code, in general. (Yes, I know
> that 'class' is an executable statement in Python; but you know what I
> mean.) When I write:
>
> myForm = widgets.TableForm(fields=MyFields(),
> submit_text="Submit")
>
> I expect 'fields' to be instantiated at the point of invocation,
> but in
> fact, it was instantiated when the class definition for 'MyFields' was
> parsed:
>
> class MyFields(widgets.WidgetsDeclaration):
> field1 = widgets.TextField()
> field2 = widgets.SingleSelect()
> ...
>
> This seems a bizarre trick to me; it would make more sense if the
> field
> assignments were done within an __init__() method, and MyFields were
> treated as a Singleton at runtime. The "tweaks" you refer to could be
> accomplished with parameters to the constructor.
>
> Maybe I'm just not well-versed in Python philosophy...
It's a bizarre trick, probably... but it's really only there to make
form declarations more *readable* (read: syntactic sugar).
Widget declarations like those are really lists. These two snippets
are equivalent:
class Fields(WidgetsList):
name = TextField()
age = TextField()
fields = Fields()
---
fields = [TextField("name"), TextField("age")]
So you can actually avoid having all widgets instantiated when the
module is loaded if that is a problem, for example:
class LazyForm(Form):
def __init__(...):
fields = [...]
super(LazyForm, self).__init__(fields=fields, ...)
This "LazyForm" will instantiate it's widgets when initialized, not
when the class is declared.
Regards,
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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
-~----------~----~----~----~------~----~------~--~---