[Baypiggies] deleting an inherited attribute?

Shannon -jj Behrens jjinux at gmail.com
Wed Aug 9 00:21:57 CEST 2006


On 8/8/06, Rob Miller <ra at burningman.com> wrote:
> hi,
>
> i have a situation where one of my classes is inheriting an attribute defined
> at the class level of one of my base classes, like so:
>
> class SuperClass:
>      value = 'Foo'
>
> class MyClass(SuperClass):
>      pass
>
> the problem is that i don't need the 'value' attribute at all, and in fact its
> very existence is causing problems with some other dependency code.  there is
> a call elsewhere that is doing something like this:
>
> myinstance = MyClass()
> if hasattr(myinstance, 'value'):
>      print "this is the wrong result"
> else:
>      print "this is what i want to happen"
>
> is there some way to remove the inherited attribute from MyClass's namespace?
>   i've tried putting 'del MyClass.value' or 'del self.value' in MyClass's
> __init__ method, but i get AttributeErrors.  'del SuperClass.value' works, of
> course, but that would then impact SuperClass itself, and all of the other
> consumers of SuperClass, which won't do.
>
> any suggestions?

It's too bad you can't fix the hasattr code.  A much nicer idiom for
this situation is:

    if getattr(myinstance, 'value', None):

Best Regards,
-jj


More information about the Baypiggies mailing list