read-only attributes

limodou limodou at gmail.com
Thu Feb 9 21:23:05 EST 2006


On 2/10/06, john peter <neuzhoundxx at yahoo.com> wrote:
> while reading the lib manual, i came across mentions of read-only
> attributes.
>  for example, a method has a read-only attribute (called _im_self ?) for
> binding
>  a class instance to the method. is such a facility available to custom
> application
>  code? if so, 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 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.")

New in version 2.2.

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit



More information about the Python-list mailing list