embarrassing class question

Jonas H. jonas at lophus.org
Thu Oct 21 20:38:27 CEST 2010

On 10/21/2010 08:09 PM, Brendan wrote:
> Two modules:
> x.py:
> class x(object):
>      pass
> y.py:
> from x import x
> class y(x):
>      pass
> Now from the python command line:
>>>> import y
>>>> dir(y)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> 'x', 'y']
> I do not understand why class 'x' shows up here.

Because that's how `import` behaves. It imports *every* member of the 
module into the importing module's global namespace (except for 
attributes that start with an underscore).

You can specify the attributes that shall be import with a star-import 
by settings __all__. In your case, you would add `__all__ = ['y']` to y.py.


More information about the Python-list mailing list