read-only attributes
john peter
neuzhoundxx at yahoo.com
Fri Feb 10 18:53:12 EST 2006
Thank you for the suggestion!
bruno at modulix <onurb at xiludom.gro> wrote: limodou wrote:
> On 2/10/06, john peter wrote:
(snip)
>> what do i have to do if i want my application code to have
>>read-only
>> attributes?
>>
> I think you may consider property() built-in function:
>
> property( [fget[, fset[, fdel[, doc]]]])
>
> Return a property attribute for new-style classes (classes that derive
> from object).
> fget is a function
s/function/callable/
> for getting an attribute value, likewise fset is a
> function for setting, and fdel a function for del'ing, an attribute.
> Typical use is to define a managed attribute x:
>
>
> class C(object):
> def __init__(self): self.__x = None
> def getx(self): return self.__x
> def setx(self, value): self.__x = value
> def delx(self): del self.__x
> x = property(getx, setx, delx, "I'm the 'x' property.")
Note that you don't need to define all three accessors. For a
'read-only' attribute, just define the getter:
class ReadOnly(object):
def __init__(self, x):
self._x = x
x = property(fget=lambda self: self._x)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list
---------------------------------
Yahoo! Mail
Use Photomail to share photos without annoying attachments.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060210/aee64d23/attachment.html>
More information about the Python-list
mailing list