embarrassing class question

Ethan Furman ethan at stoneleaf.us
Thu Oct 21 14:56:36 EDT 2010


Jonas H. wrote:
> 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).

Um, no.  (unless you do "from <whatever> import *" at the module level)

What it does is add whatever you imported into the namespace where you 
imported it.

Because y.py has "from x import x" the x class from x.py is added to the 
y.py namespace.

~Ethan~



More information about the Python-list mailing list