473,225 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 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 1583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
7
by: George Sakkis | last post by:
I was kinda surprised that setting __class__ or __dict__ goes through the __setattr__ mechanism, like a normal attribute: class Foo(object): def __setattr__(self, attr, value): pass class...
3
Kelicula
by: Kelicula | last post by:
This is NOT a complete OO perl tutorial However I thought it could be beneficial to explain some of the basic concepts, and allow some users to simplify the software design process. I have...
2
by: Jan Schilleman | last post by:
Hi all, I am trying to redefine __setattr__. The base class is in a library (actually, it is win32com.client.DispatchBaseClass) and I do not want to touch it. My problem is exemplified below....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.