How to import all things defined the files in a module directory in __init__.py?

Peng Yu pengyu.ut at gmail.com
Thu Sep 22 11:50:49 EDT 2016


On Wed, Sep 21, 2016 at 11:14 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Peng Yu <pengyu.ut at gmail.com> writes:
>
>> I want to import all the thing (or the ones available in the
>> respective __all__) defined in each of the file by putting the
>> following lines in __init__.py
>>
>> from file1 import *
>> ....
>> from filen import *
>>
>> However, I don't want to hardcode the file names in __init__.py. Is
>> there an automatic way of doing it?
>
> Why do you want to do that? It will defeat static analysis of the code,
> which is one of the more important tools to make your code reliable.
>
> It will also make the names in the code impossible to automatically
> match against where they came from. Explicit is better than implicit;
> you are proposing to make an unknown horde of names in the code implicit
> and untraceable.
>
> Why? What problem are you trying to solve that you believe needs this
> approach?

This will make refactoring easy. If everything is explicit, when one
do refactoring, at two places need to be changed which can be a
burden.

-- 
Regards,
Peng



More information about the Python-list mailing list