On Apr 3, 2009, at 12:35 PM, Chris Rossi wrote:

> Hello,

Hi Chris.

> I was wondering if the Zope collective had given any consideration  
> to allowing constants to be defined in interfaces.  To be clear,  
> these are constant values that make up the protocol defined by the  
> interface.


FWIW, interfaces.py is often regarded as a contract itself.  A current  
best practice is to define constants and exceptions in that file, and  
import them from the module.

> If this hasn't been done yet, I can envision doing something like:
> from zope.interface import Constant
> class IHttpResponse(Interface):
>     """Models an HTTP 1.1 response.
>     """
>     HTTP_OK = Constant("200 Ok", "An HTTP Ok response.")
>     HTTP_NOT_FOUND = Constant("404 Not Found", "An HTTP Not Found  
> response")
>     status = Attribute("HTTP status code for this response.")

How would that be a win for you (or anybody else) over just putting  
the constant in the interfaces module?

If it is in the interface, that implies we need to "implement" it  
someplace--the constant will be defined in the interface and in the  
implementation?  Or we would offer automation to copy the values over  
from the interface to objects that implement the interface?

> Using descriptors, the results could be both static and immutable.

Mostly static and immutable, anyway. :-)  In Python, there's almost  
always a way around absolutes like that unless you are working with  
something in which the constraint has been coded in C.

I don't find enforcing a constant's immutability in any way other than  
relying on a programmer's good sense to be particularly valuable.  I  
have sympathy for "enterprise-y" concerns, in which you have some belt- 
and-suspenders--we use security proxies for that kind of thing, and  
appreciate them despite their added pain--but I don't see how a  
programmer might "forget" to not overwrite an ALL_CAPS constant.

And finally, this "Constant" constructor would control the interface,  
not the implementation, so you'd have to do the static/immutable  
things elsewhere.

So, I'm -1 on "Constant" unless someone gives a convincing reason why  
it is preferable to putting constants in the module, in which case I  
will suddenly have a polar shift to +0. :-)

Zope-Dev maillist  -  Zope-Dev@zope.org
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope )

Reply via email to