embarrassing class question

Dave Angel davea at dejaviewphoto.com
Fri Oct 22 14:16:26 CEST 2010

On 2:59 PM, Brendan wrote:
> On Oct 21, 3:56 pm, Ethan Furman<et... at stoneleaf.us>  wrote:
>> <snip>
>> Because y.py has "from x import x" the x class from x.py is added to the
>> y.py namespace.
>> ~Ethan~- Hide quoted text -
>> - Show quoted text -
> So what is usually done to prevent this? (In my case not wanting class
> x added to the y.py namespace)
> It seems sloppy.
Since you make the common mistake of using the same name for the module 
as you do for the class, it's hard to demonstrate.  But if you used Pep8 
naming conventions, the classes would be capitalized.

Instead of using

from x import X

try using

import x

class Y(x.X):

Now, you still have a symbol x in your namespace, but it's just the 
module, which is perfectly public.  So you could access a dozen classes 
within x, but only the module itself would be visible to others.

As someone else pointed out, you could also delete the module reference 
when you're done with it.

import x

class Y(x.X):

del x


More information about the Python-list mailing list