migrating to packages

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Oct 3 22:23:37 EDT 2007


Gerardo Herzig a écrit :
> Bruno Desthuilliers wrote:
> 
>> Gerardo Herzig a écrit :
>>  
>>
>>> Carl Bank a écrit :
>>>   
>>>
>>>> Add these lines in __init__.py:
>>>>
>>>> from MYCLASSES.A import A
>>>> from MYCLASSES.B import B
>>>>
>>>>
>>>>     
>>>
>>> Ummm, that works indeed, but forces me to import all (more than A and 
>>> B) classes, rigth?
>>>   
>>
>>
>> Why so ?
>>
>>  
>>
> If the original MYCLASSES.py has 5 different classes ,say A,B,C,D,E , 
> each one has to be imported 

where ? In the __init__.py or in the client code ?

> (as A and B) in order to be used for the 
> client code.

 From the client code POV, it doesn't change anything.

> The thing is, there are more than 5 classes,

Here again : where ?

You know, it's pretty common in Python to have more than one single 
symbol publicly available from a given module, and you don't *have* to 
do the java one-class-per-file dance. The main reason for splitting an 
existing module into multiple submodules of a package is to keep 
module's size manageable. If each of your classes is so big it need a 
whole module, then you possibly have another problem...

> and looks like 
> a lot of unnecesary work to me, since a particular program can use 1,2, 
> or 3 classes at the time....

Then either it import the package and use fqn, or it only imports the 
needed classes.

> Thats why im watching the way to override 
> the `import statement'.......

IMHO, chances are you're going for the wrong solution.



More information about the Python-list mailing list