471,047 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,047 software developers and data experts.

Mixing custom __setattr__ method and properties in new style classes

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)

Feb 8 '06 #1
3 1533
I see the error: the get and set properties are inverted. Now it works.

Feb 8 '06 #2
"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>

Feb 8 '06 #3
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
sc***********@acm.org
Feb 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.