organizing your scripts, with plenty of re-use

Robert Kern robert.kern at gmail.com
Sun Oct 4 22:08:56 CEST 2009


On 2009-10-04 10:48 AM, Stef Mientki wrote:
> Steven D'Aprano wrote:
>> On Sat, 03 Oct 2009 10:24:13 +0200, Stef Mientki wrote:
>>
>>> I still don't use (because I don't fully understand them) packages, but
>>> by trial and error I found a reasonable good working solution, with the
>>> following specifications
>>
>> I find that fascinating. You haven't used packages because you don't
>> understand them, but you've used another technique that you *also*
>> don't understand well enough to generate a solution, and had to rely
>> on trial and error.
>>
>> Packages are quite well documented. Since the alternative was trial-and-
>> error on something you also don't fully understand, why did you avoid
>> packages?
>>
>>
>>
> I want to have the possibility to import any file from any other file:
>
> <quote from your other answer>
>
> parrot/
> +-- __init__.py
> +-- feeding/
> +-- __init__.py
> +-- eating.py
> +-- drinking.py
> +-- fighting.py
> +-- flying.py
> +-- sleeping.py
> +-- talking.py
> import parrot # loads parrot/__init__.py
> import parrot.talking # loads parrot/talking.py
> from parrot import sleeping
> import parrot.feeding
> from parrot.feeding.eating import eat_cracker
>
> </quote>
>
> Instead of the above:
> from sleeping import sleeping_in_a_bed
> from eating import eat_cracker
>
> anything wrong with that (knowing I've no redundancy) ?

With the package layout, you would just do:

   from parrot.sleeping import sleeping_in_a_bed
   from parrot.feeding.eating import eat_cracker

This is really much more straightforward than you are making it out to be.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list