I have a class like this:
class Foo(object):
def __init__(self):
self.bar = property(self.get_bar)
def get_bar(self):
return \"bar\"
pr
You can also do it like shown here:
class Foo(object):
def __init__(self):
self._bar = None
@property
def bar(self):
return self._bar
@bar.setter
def bar(self, value):
self._bar = value
@bar.deleter
def bar(self):
self._bar = None # for instance
which is equivalent to:
class Also_Foo(object):
def __init__(self):
self._bar = None
def get_bar(self):
return self._bar
def set_bar(self, value):
self._bar = value
def del_bar(self):
self._bar = None # for instance
bar = property(fget=get_bar, fset=set_bar, fdel=del_bar, doc=None)
BUT without polluting the class namespace with get
and set
methods for each attribute.
You retain external direct access to the variable by using ._bar
instead of .bar
.