[Zope3-dev] __init__.py interfaces.py guidelines?

Jim Fulton jim at zope.com
Tue Nov 22 09:57:07 EST 2005


Gary Poster wrote:
> 
> On Nov 21, 2005, at 12:29 PM, Jean-Marc Orliaguet wrote:
> 
>>
>> There is another place where there seems to be two different  patterns 
>> too:
>>
>> sometimes we have:
>>
>>   import zope.schema
>>   name = zope.schema.TextLine(...)
>>
>> and sometimes:
>>
>>   from zope.schema import TextLine
>>   name = TextLine(...)
> 
> 
> FWIW, a third is
> 
>   from zope import schema
> 
> which I often do for zope.component, zope.interface, zope.event, and  
> zope.schema.
> 
> I'm not weighing in on the style issues.

FWIW:

- For obscure things that are used only a few times in a module,
   and especially for things that are used only during module import,
   I prefer the first form.  This makes the code easier to read because you
   don't have to refer to the imports.  It also tends to make the imports
   shorter and easier to manage.

- For popular modules like component, interface, and schema, I strongly
   prefer the thirs option because it keeps the imports simpler, and
   falls between the two extremes.

   A variation of the the third option that I also like for similar reasons
   as above occurs when importing sibling modules within the same package.
   For example, suppose we have modules foo and interfaces within mypackege.
   In foo, if I wanted to get to interfaces, I would use:

      from zope import interface
      from mypackage import interfaces
      ....

      class Foo:
          interface.implements(interfaces.IFoo)

   This gives us the same effect as if we had done a relative import of
   interfaces:

      import interfaces

   without breaking the no-relative-import rule.

- I prefer the second form for well-known objects that are
   used a lot.

For cases that fall between these extremes, I tend to
prefer the first form.

I think this is a matter of taste and art.  I'm not very keen
to legislate this. :)

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Zope3-dev mailing list