embarrassing class question

Chris Rebert clp2 at rebertia.com
Thu Oct 21 21:02:16 CEST 2010


On Thu, Oct 21, 2010 at 11:53 AM, Brendan <brendandetracey 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.
>>
>> Carl Banks
>
> So it must never make sense to put subclasses in separate modules?

Limiting yourself to one class per module is neither mandatory nor
common in Python. Python is not Java.

Cheers,
Chris



More information about the Python-list mailing list