[Zope3-dev] zope.schema: defaults for non-immutables... questions

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Feb 5 12:20:30 EST 2006

On Tuesday 24 January 2006 12:26, Shaun Cutts wrote:
> It would seem that the current default mechanism is poorly suited to
> providing default values for non-immutables. For example:
> class IBar( Interface ):
>     a = Object( schema = IFoo, default = Foo() )

Yes, this is even more apparent with the Datetime object, where you often want 
to make the default "now".

> A proposal to remedy: if the default is a callable object, it is assumed
> to be a factory.

That is dangerous. What if a value is really a callable.
> How does this sound? (This would apply to missing_value as well.)

I would prefer a new defaultFactory argument, which would be more 
backward-compatible too.

> A few further questions:
> 1) who should be responsible for setting defaults, and how should it be
> done?
> I have a base class from which I derive (e.g.) Bar, whose constructor
> loops through fields of interfaces provided by the instance:
>         for iface in zope.interface.providedBy( self ):
>             for fname, field in zope.schema.getFields( iface
> ).iteritems():
> It checks default & missing_value, and sets them. However, if one field
> shadows another in the interfaces, there is no guarantee that I hit them
> in the right order. Might it not be good to have a
> "attributesProvidedBy( instance ) in zope.interface that guarantees that
> it passes back the most derived versions (and resolves consistently when
> there is no "most derived" )?

Usually we do not enforce defaults like that. Defaults are provided by the 
constructor arguments. The default value is really only used for the UI.

> Also, currently, when something doesn't have a definition, and is not
> required, I check first for "default" then for "missing_value", and use
> the first I find as a missing value. Is this correct?

Yes, I think so.

> How should 
> "default" and "missing_value" interact with each other? What makes most
> sense to me is: (pseudocode):
> If not specified:
>     If default defined
>         Use default
>     Else
>         If required
>             Raise error
>         Else
>             If missing value defined
>                 Use missing value
>             Else
>                 Raise error
> Finally, a missing missing_value eventually gets "None" in the current
> Field constructor. Shouldn't it be left undefined when it wasn't
> specified? (Same for default.)

I think using None as a default for missing_value is ok, so the only thing I 
would change in your pseudo code is to change the last error raising to:

missing_value = None

BTW, I am really glad someone is looking at this code. It has not gotten the 
attention it deserves. Thank you very much! 

I really hope you will get checkin rights and check in your suggestions! :-)

Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training

More information about the Zope3-dev mailing list