[Tutor] Inheritance in classes

David Palao dpalao.python at gmail.com
Tue Apr 8 10:49:43 CEST 2014


2014-04-08 7:44 GMT+02:00 Santosh Kumar <rhce.san at gmail.com>:
> 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
>    ...:
>
> In [3]: obj1 = b()
>
> In [4]: obj1.value1
> Out[4]: 1
>
> In [5]: obj1.value2
> Out[5]: 2
>
> 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. Is there a way
> by which i can make the child class not inherit some of the properites of
> parent class.
>
>
> --
> D. Santosh Kumar
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

Hi,
This is the default behaviour. You can ovrcome that, but it will
require extra work. For instance you could make use of pseudoprivate
attributes (any attribute starting with double underscore, but not
ending with dtwo underscores); or some managing attributes tool:
*) __getattr__, __getattribute__ are generic ways to manage attribute fetching
*) properties and descriptors allow a more specific way to control
attributes (one by one)
But, be careful. These tools can be very tricky at first.
Hope it helps.

Best


More information about the Tutor mailing list