Leo Shklovskii <[email protected]> writes:

> "the globals in a module are equivalent to that module's attributes. The 
> globals 
> of foo/__init__.py correspond to the attributes of the "foo" package. 
> Importing 
> foo.cars causes the import machinery to assign the imported module object to 
> foo.cars."

Correct. Global name lookup in a module uses the exact same dictionary
as is used to pull attributes out of a module.

> The real issue that we hit is that if you defined 'cars' in foo/__init.py__, 
> the 
> line 'from foo.cars import blah' would clobber whatever you defined there. 

Correct again.

> The takeaway is don't define anything in __init__.py :-/

It does have its uses sometimes, though you're right in that it's not
often a good idea to do so. Importing things from modules in the same
package (i.e. doing `from foo.cars import vroom` in foo/__init__.py) can
be useful.

So, if you're going to put code in __init__.py, make it imports and not
definitions.

Reply via email to