Re: read-only attributes

Discussion in 'Python' started by limodou, Feb 10, 2006.

  1. limodou

    limodou Guest

    On 2/10/06, john peter <> 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
    limodou, Feb 10, 2006
    #1
    1. Advertising

  2. 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 ''.split('@')])"
    bruno at modulix, Feb 10, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    2
    Views:
    7,991
    keithb
    Jun 7, 2006
  2. Paul Moore
    Replies:
    9
    Views:
    592
    Roy Smith
    Feb 21, 2005
  3. Samuel M. Smith
    Replies:
    6
    Views:
    334
    Samuel M. Smith
    Dec 9, 2005
  4. Tim Chase
    Replies:
    2
    Views:
    323
    James Stroud
    Mar 15, 2006
  5. Thomas Guettler
    Replies:
    1
    Views:
    296
    Peter Otten
    Nov 25, 2009
Loading...

Share This Page