[Zope3-Users] Re: Recipe with default values

Philipp von Weitershausen philipp at weitershausen.de
Sun Feb 19 11:29:47 EST 2006


Marco Mariani wrote:
> In chapter 5.4 of the cooking book, Philipp says:
> 
>> [...] We could now implement a custom factory, for example one that
>> allows us to set initial attribute values of the recipe. (We would
>> normally extend the Recipe class with a constructor to do that, but
>> let us implement a factory here for demonstration purposes.)

Note that this particular example of a custom factory is a bad example.
It has been argued that factories should preferrably not take arguments.
If they did take certain (required) arguments, then they would be
implying a contract that isn't explicitly stated in an interface.

> How should I write the constructor? I've tried the naive __init__ method
> but I must be missing something, as it doesn't get passed any argument.
> 
> For instance, I'd like to have an attribute recipe.creation_date which
> is editable in the addform but has datetime.datetime.now() as a default
> value.

I presume, this is what you want:

  class Recipe(Persistent):

    def __init__(self):
        self.creation_date = datetime.datetime.now()

Philipp



More information about the Zope3-users mailing list