Dynamic class problem
Robin Becker
robin at jessikat.fsnet.co.uk
Tue Jun 5 16:06:56 EDT 2001
I'm trying to figure out how to recode a class that is giving a shadow warning under 2.1
the relevant code is
def __getitem__(self, index):
try:
return self._children[index]
except KeyError:
Klass = self._prototype
if Klass in _ItemWrapper.keys():
WKlass = _ItemWrapper[Klass]
else:
class WKlass(Klass):
def __getattr__(self,name):
try:
return Klass.__getattr__(self,name)
except:
return getattr(self._parent,name)
_ItemWrapper[Klass] = WKlass
child = WKlass()
child._parent = self
for i in filter(lambda x,K=child.__dict__.keys(): x in K,child._attrMap.keys()):
del child.__dict__[i]
self._children[index] = child
return child
the idea is that I want to generate a child instance based on the _prototype class which has a
single overridden getattr method. This code seemed to be working in 2.0, but I'm getting
warnings now
c:\python\reportlab\graphics\widgetbase.py:181: SyntaxWarning: local name 'Klass
' in '__getitem__' shadows use of 'Klass' as global in nested scope 'WKlass'
def __getitem__(self, index):
must I write the inner class as
class WKlass(Klass):
def __getattr__(self,name):
try:
return WKlass.__bases__[0].__getattr__(self,name)
except:
return getattr(self._parent,name)
and is that backwards compatible in 2.0?
--
Robin Becker
More information about the Python-list
mailing list