Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mixing custom __setattr__ method and properties in new style classes

Reply
Thread Tools

Mixing custom __setattr__ method and properties in new style classes

 
 
L.C. Rees
Guest
Posts: n/a
 
      02-08-2006
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)

 
Reply With Quote
 
 
 
 
L.C. Rees
Guest
Posts: n/a
 
      02-08-2006
I see the error: the get and set properties are inverted. Now it works.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-08-2006
"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>



 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      02-08-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__getattr__, __setattr__ and pickle Bruno Desthuilliers Python 6 08-13-2008 12:34 PM
mixing C extension classes with ruby classes snacktime Ruby 1 09-20-2006 02:27 PM
new style and classic style classes ? ankit Python 1 12-22-2005 08:34 AM
Recursion with __setattr__ Chris Young Python 0 10-16-2005 07:55 AM
__setattr__ and __getattr__ with derived classes Anand Python 0 12-18-2003 09:36 PM



Advertisments