embarrassing class question

Carl Banks pavlovevidence at gmail.com
Thu Oct 21 20:47:26 CEST 2010


On Oct 21, 11:09 am, Brendan <brendandetra... at yahoo.com> 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 you imported it into the namespace, which is what the import
statement does.  dir() shows you what's in the namesace; therefore it
lists x.  dir() doesn't care, and can't know, if something was defined
in a namespace, or merely imported.

If it bothers you, you can put "del x" after the class y definition,
but I recommend against doing that in general.  If there's a reference
to x inside a function that function will raise an exception if
called, because it expects x to be inside the namespace.


Carl Banks



More information about the Python-list mailing list