[sugar] Python coding conventions

Marco Pesenti Gritti mpg
Mon Oct 2 08:19:27 EDT 2006


Ian Bicking wrote:
> 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.
>

I tried to use lower case names for modules and it works better. Unless 
Dan disagree I think we should go with this style for modules. Ian, it 
would be great if you could come up with a simple code style document.

Marco


More information about the Sugar-devel mailing list