[sugar] Python coding conventions
Ian Bicking
ianb
Thu Sep 28 12:11:38 EDT 2006
Marco Pesenti Gritti wrote:
> Eeeeh here comes my hate for python imports. What I'd really want in the
> code is:
>
> from sugar import presence
>
> ps = presence.PresenceService()
>
> Now, is there a way to get that without stuffing all the presence
> service in presence.py. Or more in general can you elaborate on a way to
> organize code in python that doesn't suck :) I guess we have been trying
> to emulate java like packaging with generally one class per file, but
> that doesn't seem to play well with python imports.
>
> Anyway it would be great to solve this in a good way. I hate what we
> have currently.
You can put everything in presence/__init__.py. I don't like using
__init__.py this way, though; it's a little quirky as a file
(ImportError's in it are swallowed), and by itself it has a rather
ambiguous name. So I typically put something like this in there:
# presence/__init__.py
from presenceservice import PresenceService
Then you can do "from presence import PresenceService". When you then
try to track down PresenceService you'll have a little indirection, but
usually it's not too distracting.
One downside is that there is then no way to import (and hence load)
some of the code without importing all of it. I.e., if you wanted
access to something in presence/ but not PresenceService, you would end
up implicitly importing PresenceService whether you wanted to or not.
--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org
More information about the Sugar-devel
mailing list