How to import only one module in a package when the package __init__.py has already imports the modules?

Peng Yu pengyu.ut at gmail.com
Sun Nov 1 04:34:03 CET 2009


On Sat, Oct 31, 2009 at 9:42 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> On Sat, 31 Oct 2009 20:03:29 -0500, Peng Yu wrote:
>
>>> If it should ever happen that two functions are too long to put in a
>>> single file you should refactor your code. It is usually a good idea of
>>> breaking problems down into single steps (ie functions) so you never
>>> end up with a 5000 SLOC *function*.
>>
>> My definition of long is more than one screen.
>>
>>> How do functions of this length enhance the readability of your source
>>> code?
>>
>> If the code is of one screen, you can easily see what it does without
>> having to scroll back and forth.
>
> Far enough, but you exchange scrolling back and forth from function to
> function with tabbing through editor tabs from file to file. I don't see
> that this is an advantage.
>
> You also complicate calling functions. Instead of:
>
>
> def parrot():
>    spam()
>    ham()
>    cheeseshop()
>
>
> you need:
>
>
> from spam import spam
> from ham import ham
> from import cheeseshop
>
> def parrot():
>    spam()
>    ham()
>    cheeseshop()
>
>
> This is not an advantage!

I would say this is the disadvantage of python, if there is no
walkaround to avoid using 'from spam import spam'. As this can be
handled in C++ with walkaround.

So far the best solution is:

Inside test/__init__.py:

from a import A  # class A from file a.py
from b import B  # class B from file b.py

However, this is not satisfactory enough because this artificially
introduces the constraint that the file name can not be the same as
the class/function name.



More information about the Python-list mailing list