Mixing custom __setattr__ method and properties in new style classes

Discussion in 'Python' started by L.C. Rees, Feb 8, 2006.

  1. L.C. Rees

    L.C. Rees Guest

    Can custom __setattr__ methods and properties be mixed in new style
    classes?

    I experimented with a new style class with both a custom __setattr__
    method and a property. Attributes controlled by the __setattr__ method
    work fine. When I attempt to set or get the property it raises the
    error: "TypeError: _settext() takes exactly two arguments (1 given)".
    This suggests that the __setattr__ may be conflicting with assignment
    to the property since it doesn't seem the class name is being passed to
    the property when it's created. This is the code for the property at
    the end of the class definition:

    def _settext(self, txt):
    self._tree.text = txt

    def _gettext(self, txt):
    return self._tree.text

    def _deltext(self, txt):
    self._tree.text = ''

    text = property(_settext, _gettext, _deltext)
    L.C. Rees, Feb 8, 2006
    #1
    1. Advertising

  2. L.C. Rees

    L.C. Rees Guest

    I see the error: the get and set properties are inverted. Now it works.
    L.C. Rees, Feb 8, 2006
    #2
    1. Advertising

  3. Re: Mixing custom __setattr__ method and properties in new styleclasses

    "L.C. Rees" wrote:

    > This is the code for the property at the end of the class definition:
    >
    > def _settext(self, txt):
    > self._tree.text = txt
    >
    > def _gettext(self, txt):
    > return self._tree.text
    >
    > def _deltext(self, txt):
    > self._tree.text = ''
    >
    > text = property(_settext, _gettext, _deltext)


    hint:

    >>> help(property)


    class property(object)
    | property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
    |
    | fget is a function to be used for getting an attribute value, and 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 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.")

    </F>
    Fredrik Lundh, Feb 8, 2006
    #3
  4. Re: Mixing custom __setattr__ method and properties in new styleclasses

    L.C. Rees wrote:
    > Can custom __setattr__ methods and properties be mixed in new style
    > classes?
    >
    > I experimented with a new style class with both a custom __setattr__
    > method and a property. Attributes controlled by the __setattr__ method
    > work fine. When I attempt to set or get the property it raises the
    > error: "TypeError: _settext() takes exactly two arguments (1 given)".
    > This suggests that the __setattr__ may be conflicting with assignment
    > to the property since it doesn't seem the class name is being passed to
    > the property when it's created. This is the code for the property at
    > the end of the class definition:
    >
    > def _settext(self, txt):
    > self._tree.text = txt
    >
    > def _gettext(self, txt):
    > return self._tree.text
    >
    > def _deltext(self, txt):
    > self._tree.text = ''
    >
    > text = property(_settext, _gettext, _deltext)
    >

    The order to call property is get, set, del, doc
    You need to take a _lot_ more care before asking for help.
    neither get not del take any arg besides self. When you are
    trying to debug an interaction, make sure your tests work stand-
    alone.

    class Holder(object): pass # to allow attributes stuck on

    class Demo(object):
    def __init__(self):
    self._tree = Holder()

    def _settext(self, txt):
    self._tree.text = txt

    def _gettext(self):
    return self._tree.text

    def _deltext(self):
    self._tree.text = ''

    text = property(_gettext, _settext, _deltext, 'text property')

    d = Demo()
    d.text = 'my text'
    print repr(d.text)
    del d.text
    print repr(d.text)

    --Scott David Daniels
    Scott David Daniels, Feb 8, 2006
    #4
    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. Anand
    Replies:
    0
    Views:
    392
    Anand
    Dec 18, 2003
  2. Chris Young

    Recursion with __setattr__

    Chris Young, Oct 16, 2005, in forum: Python
    Replies:
    0
    Views:
    361
    Chris Young
    Oct 16, 2005
  3. ankit
    Replies:
    1
    Views:
    328
    Alex Martelli
    Dec 22, 2005
  4. Bruno Desthuilliers

    __getattr__, __setattr__ and pickle

    Bruno Desthuilliers, Aug 12, 2008, in forum: Python
    Replies:
    6
    Views:
    363
    Bruno Desthuilliers
    Aug 13, 2008
  5. snacktime
    Replies:
    1
    Views:
    107
    Nobuyoshi Nakada
    Sep 20, 2006
Loading...

Share This Page