Chris Rossi wrote:

> 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.")
> Using descriptors, the results could be both static and immutable.
> Does this seem useful to anyone besides me?  Anyone who's done much Java 
> programming will recognize that I did not have an original idea here. 


I think having a more robust way to spell and namespace constants may be 
interesting, but what we see above, with a mixed interface/class, is 
actually not going to work given zope.interface's semantics. You'd end 
up with an interface that promised an attribute that didn't exist.

In Java, you can do:

  interface IFoo {
      public static String FOO = "foo";

  class Foo implements IFoo {}

  f = Foo();


In Python with zope.interface, implements() doesn't mean you inherit 
attributes. Therefore, you'd need to do:

  class IFoo(Interface):
      FOO = Constant("foo")

  class Foo(object):
      FOO = "foo" # or FOO = IFoo['FOO'].get() or something

  f = Foo()
  print f.FOO

Without repeating the definition of the FOO constant in each class 
implementing IFoo, the interface would be promising an attribute that 
didn't exist. validateInterface() would complain too.

In general, I don't have a problem with doing constants that are not, 
strictly speaking, immutable. I tend to put them in if 
they are part of the "contract" of my package. Codifying that as good 
practice is probably a good idea. We certainly could come up with some 
other way of spelling this, e.g.

  class FooConstants(Constants):
      FOO = Contant("Foo")

or whatever... maybe some way to mix in zope.schema to to describe the 
fields in more details. But it feels like a fair amount of situps when 
you can just do FOO = "Foo" at module level and be done with it. Python 
is a dynamic language. We don't have privates or final methods or lots 
of other things that you can spell in a strictly typed, compiled 
language like Java.


Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See

Zope-Dev maillist  -
**  No cross posts or HTML encoding!  **
(Related lists - )

Reply via email to