[Tutor] subclass problem: __names and type-checking

Python python at venix.com
Sat Oct 8 19:40:32 CEST 2005

Traceback (most recent call last):
   File "C:/Python24/foofoofoo.py", line 26, in -toplevel-
     s2 = Sub2()
   File "C:/Python24/foofoofoo.py", line 22, in __init__
     super(Sub2, self).__init__()
   File "C:/Python24/foofoofoo.py", line 10, in __init__
     if type(self) == __TwoUnderBase:  # What to write here
NameError: global name '_TwoUnderBase__TwoUnderBase' is not defined

Within a class, __ prefixes mangle the name to include the class name as
part of the name.  However, it appears that within a class statement,
the __ prefix is not mangled.  This is good for you because otherwise
the inheritance would not work.  However, it appears to make it
impossible to directly reference the class name from within the class.
You may want to rethink the idea of using __ prefixes in the class name.
Normally __ is used to "privatize" attributes within a class.

Lloyd Kvam
Venix Corp

More information about the Tutor mailing list