[Tutor] inheriting private variables...

ali mangaliag rmangaliag@slu.edu.ph
Wed Jun 4 04:54:01 2003


This is a multi-part message in MIME format.

------=_NextPart_000_00D9_01C32ABB.8BB8C960
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

if i do have a class defined like this:

class a:
    def __init__(self):
        self.__prv_var =3D 1     ### a.k.a =3D=3D> _a__prv_var
    def get_prv_var(self): =20
        return self.__prv_var

and i inherited the class above like this:

class b(a):
    def __init__(self):
        pass

and if instantiate class b below...

x =3D b()

and call the inherited method get_prv_var... like so...

print x.get_prv_var()

this will result to an error message... saying that _a__prv_var is not =
defined...
though this will work fine if the attribute was defined as =
self.prv_var.....

my question is, with object-oriented data hiding and encapsulation in =
mind... how can i define a private
variable and subsequently let class inherit it... and refer to it just =
like any attribute???



------=_NextPart_000_00D9_01C32ABB.8BB8C960
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>if i do have a class defined like=20
this:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class a:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; def =
__init__(self):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
self.__prv_var =3D 1 &nbsp;&nbsp;&nbsp; ### a.k.a =3D=3D&gt; =
_a__prv_var</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; def =
get_prv_var(self):&nbsp;=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
return=20
self.__prv_var</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>and i inherited the class above like=20
this:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class b(a):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; def =
__init__(self):</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
pass</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>and if instantiate class b =
below...</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>x =3D b()</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>and call the inherited method =
get_prv_var... like=20
so...</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>print&nbsp;x.get_prv_var()</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>this will result to an error message... =
saying that=20
_a__prv_var is not defined...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>though this will work fine if the =
attribute was=20
defined as self.prv_var.....</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>my question is, with object-oriented =
data hiding=20
and encapsulation in mind... how can i define a private</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>variable and subsequently let class =
inherit it...=20
and refer to it just like any attribute???</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_00D9_01C32ABB.8BB8C960--