deleting an inherited attribute?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

deleting an inherited attribute?

Rob Miller
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?

-r

_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: deleting an inherited attribute?

Keith Dart-2
Rob Miller wrote the following on 2006-08-08 at 14:47 PDT:
===
> any suggestions?

===

Can you fix the other code that's checking for the attribute? If not,
you can do this:


class C(object):
    def _nope(self):
        raise AttributeError
    value = property(_nope)



--

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Keith Dart <[hidden email]>
   public key: ID: 19017044
   <http://www.dartworks.biz/>
   =====================================================================
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: deleting an inherited attribute?

Rob Miller
Keith Dart wrote:

> Rob Miller wrote the following on 2006-08-08 at 14:47 PDT:
> ===
>> any suggestions?
>
> ===
>
> Can you fix the other code that's checking for the attribute? If not,
> you can do this:
>
>
> class C(object):
>     def _nope(self):
>         raise AttributeError
>     value = property(_nope)

no, but this seems to work well.  i would have thought that the existence of
the property would have still caused hasattr to return True, but the
AttributeError seems to negate that.

thanks!

-r

_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: deleting an inherited attribute?

Shannon -jj Behrens
In reply to this post by Rob Miller
On 8/8/06, Rob Miller <[hidden email]> 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
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies