get/set

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri May 10 13:48:57 EDT 2002


> it means that if i want to keep working with python 2.1 i need to use my
> own get/set method ?
> 

yes you do.  But realize this is not C++ or Java.  There are no private members.

Class Sneaky:
  def __init__(self):
        self.a = 0
        self.b = 0

  def getA(self): return self.a
  def setA(self, val): self.a = val # normally would have checks

foo = Sneaky()
foo.a = 'Sean was here!'

is 100% valid.  Will you ever do it, likely not.  But the key is, in python
data hiding is by social contract not language controlled.

The python idiom is usually to only have meothds when data checking is required
or the value is computed.





More information about the Python-list mailing list