embarrassing class question
pavlovevidence at gmail.com
Thu Oct 21 21:18:15 CEST 2010
On Oct 21, 11:53 am, Brendan <brendandetra... at yahoo.com> wrote:
> On Oct 21, 3:47 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> > 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.
> So it must never make sense to put subclasses in separate modules?
Putting a subclass in the same module simply because you don't want it
to appear in some other module's namespace is not a good idea, IMO.
Sometimes it makes sense to put the subclass in another module, it
depends on the organization of the project.
I'm not sure how familiar you are with Python's object and namespace
systems. I assumed you were using dir() to see what objects were
defined in a module, and were disappointed to see x in the listing
also, but maybe you're under the impression that there is a lot of
duplicated data? Not really: a namespace contains references
(pointers) and so takes up a minimal amount of space, not worth
More information about the Python-list