embarrassing class question

Robert Kern robert.kern at gmail.com
Thu Oct 21 21:04:47 CEST 2010


On 10/21/10 1:53 PM, Brendan 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.
>>
>> Carl Banks
>
> So it must never make sense to put subclasses in separate modules?

Of course it can make sense to put subclasses in separate modules, just for 
other reasons.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list