Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove class attribute in inherited class Python

Consider such code:

class A ():
   name = 7
   description = 8
   color = 9

class B(A):
   pass

Class B now has (inherits) all attributes of class A. For some reason I want B not to inherit attribute 'color'. Is there a possibility to do this?
Yes, I know, that I can first create class B with attributes 'name' and 'description' and then inherit class A from B adding attribute 'color'. But in my exact case, B is actually a reduced version of A, so for me it seems more logical to remove attribute in B (if possible).

like image 349
Graf Avatar asked Sep 09 '10 07:09

Graf


People also ask

How do you delete a class variable in Python?

Python's del statement is used to delete variables and objects in the Python program. Iterable objects such as user-defined objects, lists, set, tuple, dictionary, variables defined by the user, etc. can be deleted from existence and from the memory locations in Python using the del statement.

Are class attributes inherited?

Objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes. The resulting classes are known as derived classes or subclasses. A subclass “inherits” all the attributes (methods, etc) of the parent class.

How do you delete an attribute in Python?

The delattr() method is used to delete the named attribute from the object, with the prior permission of the object. Syntax: delattr(object, name): The function takes only two parameter: object: from which the name attribute is to be removed.

How do I remove a property from a Python object?

You can delete the object property by using the 'del' keyword.


2 Answers

I think the best solution would be to change your class hierarchy so you can get the classes you want without any fancy tricks.

However, if you have a really good reason not to do this you could hide the color attribute using a Descriptor. You'll need to be using new style classes for this to work.

class A(object):
    name = 7
    description = 8
    color = 9

class Hider(object):
    def __get__(self,instance,owner):
        raise AttributeError, "Hidden attribute"

    def __set__(self, obj, val):
        raise AttributeError, "Hidden attribute"

class B(A):
    color = Hider()

You'll then get an AttributeError when you try to use the color attribute:

>>> B.color
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __get__
AttributeError: Hidden attribute
>>> instance = B()
>>> instance.color
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __get__
AttributeError: Hidden attribute
>>> instance.color = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in __set__
AttributeError: Hidden attribute
like image 91
Dave Webb Avatar answered Oct 23 '22 20:10

Dave Webb


You can supply a different value for color in B, but if you want B not to have some property of A then there's only one clean way to do it: create a new base class.

class Base():
    name = 7
    description = 8

class A(Base):
    color = 9

class B(Base):
    pass
like image 8
Duncan Avatar answered Oct 23 '22 20:10

Duncan