[Tutor] Inheritance in classes

Alan Gauld alan.gauld at btinternet.com
Tue Apr 8 10:27:59 CEST 2014


On 08/04/14 06:44, Santosh Kumar wrote:
> Can i mask the parent attibutes in the child. let me give a quick example.
>
> In [1]: class a:
>     ...:     value1 = 1
>     ...:     value2 = 2
>     ...:
>
> In [2]: class b(a):
>     ...:     value3 = 3
>     ...:
>

Note that these are class variables and not instance
variables.


> In [3]: obj1 = b()
>
> In [4]: obj1.value1
> Out[4]: 1
>
> In [6]: obj1.value3
> Out[6]: 3
>
> If you notice in the below example you will see that the child class
> object ``obj1`` has inherited all the attibutes of the parent class.

Yes that's what inheritance means.

> there a way by which i can make the child class not inherit some of the
> properites of parent class.

No.
But you can change the inherited values by masking them with your local 
versions, which could be None.

class c(a):
    value1 = None

obj2 = c()
print(obj2.value1)  -> None


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list