python: cooperative supercall of __getattr__
I'm working with somethign similar to this code:
class BaseClass(object):
def __getattr__(self, attr):
return lambda:'1'
class SubClass(BaseClass):
suffix = '2'
def foo(self):
return super(SubClass, self).foo() + self.suffix
class SubClass2(SubClass):
suffix = '3'
def foo(self):
return super(SubClass2, self).foo() + self.suffix
o = SubClass2()
print o.foo()
I'd expect to see output of '123', but I instead I get an error
AttributeError: 'super' object has no attribute 'foo'. Python isn't even
attempting to use the base class's __getattr__.
Without modifying the base class, and keeping the two super calls similar,
I'm not able to get the output I want. Is there any cooperative supercall
pattern that will work for me here?
I understand that super() overrides getattr in some way to do what it
needs to do, but I'm asking if there's any reasonable workaround that
allows a subclass's __getattr__ to be called when appropriate.
No comments:
Post a Comment