473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Property

I'm trying to implement a simple repeateable property mechansism so I
don't have to write accessors for every single instance variable I have.
------------
classMyObject:
def __init__ (self):
self.initialize()

def initialize(self):
self._value=None
def _setProperty (self, name, value):
print "set property"
setattr (self, name, value)

def _getProperty (self, name):
print "get property"
return getattr (self, name)

#properties
value = property (lambda self: self._getProperty("_value"),
lambda self, value: self._setProperty("_value",
value))
def testObject():
o = MyObject()
print o.__dict__
print o.value
o.value = 123
print o.value
print o.__dict__

if __name__ == "__main__":
testObject()
---------

The outout looks like this
------------
{'_value': None}
get property
None
123
{'_value': None, 'value': 123}
-----------
As you can see, the _getProperty() method gets called properly when I do
'o.value' but 'o.value = 123' does not seem to use the property
stucture. I can't figure out why 'o.value=123' does not call
_setProperty()

Any ideas?

Jay
Feb 25 '06 #1
7 1475
* none wrote:
classMyObject: [...]
As you can see, the _getProperty() method gets called properly when I do
'o.value' but 'o.value = 123' does not seem to use the property
stucture. I can't figure out why 'o.value=123' does not call
_setProperty()

Any ideas?


property only works as intended with new style classes.

nd
--
Muschelflucht-Zusatzeinrichtung.

Shell-Escape ist ja noch klar, aber `Zusatzeinrichtung'?

extension?

Feature. -- gefunden in de.org.ccc
Feb 25 '06 #2
André Malo wrote:
* none wrote:

classMyObject:


[...]

As you can see, the _getProperty() method gets called properly when I do
'o.value' but 'o.value = 123' does not seem to use the property
stucture. I can't figure out why 'o.value=123' does not call
_setProperty()

Any ideas?



property only works as intended with new style classes.


duh.. I changed that to
class MyObject(object):
and it worked fine

Thanks

Take care,
Jay
Feb 25 '06 #3
André Malo wrote:
* none wrote:

classMyObject:


[...]

As you can see, the _getProperty() method gets called properly when I do
'o.value' but 'o.value = 123' does not seem to use the property
stucture. I can't figure out why 'o.value=123' does not call
_setProperty()

Any ideas?



property only works as intended with new style classes.


duh.. I changed that to
class MyObject(object):
and it worked fine

Thanks

Take care,
Jay
Feb 25 '06 #4
"none <"@bag.python.org wrote:
I'm trying to implement a simple repeateable property mechansism so I
don't have to write accessors for every single instance variable I have.


Please don't do that. The Python way is to use direct access to instance
variables unless there's a good reason not to.

Is there some reason why you want to run get/set code for every instance
variable access, or are you just experimenting to see whether it can be
done?

It seems particularly odd to want to put getters and setters behind
property access. What does the extra layer buy you?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 25 '06 #5
Em Sáb, 2006-02-25 Ã*s 09:14 -0500, Steve Holden escreveu:
It seems particularly odd to want to put getters and setters behind
property access. What does the extra layer buy you?
I can only think of some kind of debugging. Maybe?
regards
Steve


Cya,
Felipe.

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Feb 25 '06 #6
"none <"@bag.python.org wrote:
I'm trying to implement a simple repeateable property mechansism so I
don't have to write accessors for every single instance variable I have. ....
Any ideas?


Yes, don't write accessors for every single instance variable you have.
In some languages that might be appropriate, or considered good style.
In Python, it's entirely unnecessary and you should just access
instance variables directly when you want to, and in the odd case where
you want to do something more than set/get/del them you can resort to
properties (when a nice clear method wouldn't fit).

(Sorry not to answer the question directly. If you feel you really need
to use accessors then go ahead, but I just wanted you to know that many
Python programmers feel quite differently about them than, say, C++ or
Java programmers might.)

-Peter

Feb 25 '06 #7
Steve Holden wrote:
"none <"@bag.python.org wrote: It seems particularly odd to want to put getters and setters behind
property access. What does the extra layer buy you?


The purpose is that there is more to the accessors then I posted.

The setters do some 'mark dirty' bookeeping whenever the object state
changes. (I had coded something similar in a prior project but had
forgotten a bit of my own work...in that project the setters also did
some event signaling as the objects were part of an Observer-Observable
pattern)

The getters also have some code in them for dealing with default values
if the given variable behind the property does not exist (which happened
when I was pickling objects and the class structure changed over time;
it was helpful to be able to have the getter be able to check if the
variable existed and, if not, provide a default value...a way of
migrating objects to new class definitions)

So, the use of properties allowed me let clients of the class to use
direct access syntax... o.value versues o.value() or o.value = 123
versus o.value(123) ...but still allow me to do the bookkeeping needed
for my object state. The use of the _getProperty() and _setProperty()
and using lambdas in the actual property definition allowed me to have a
default setter/getter of sorts so I didn't need to write a seperate
getter and setter method for each variable

Take care,
Jay
Feb 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Filips Benoit | last post by:
Dear All, W2000 Office2000 Access adp SQLserver DB Problem: Adding a new property for a company in the subform. The FIRST time I Select a property in combobox CPROP_PRP_ID the subform act
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
0
by: Filips Benoit | last post by:
Dear All, W2000 Office2000 Access adp SQLserver DB Problem: Adding a new property for a company in the subform. The FIRST time I Select a property in combobox CPROP_PRP_ID the subform act
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
0
by: Mike Hofer | last post by:
Hi everyone. I could really use some help. First, the backstory: ===================== I *really* need a 3-state checkbox for my ASP.NET application. Specifically, I need one that lets me set...
1
by: harsha reddy | last post by:
Dear All, I have a front end form which has a couple of dropdown menus which select data from the database based on the value selected, the stored procedure which the program accesses outputs...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
2
by: Michael | last post by:
Dear All : I have a problem about define property GridTableStylesCollection in usercontrol. The code show as follow : Public Property StylesCollector() As GridTableStylesCollection Set(ByVal...
0
by: Michael | last post by:
Thanks for your advice. However, I have copy your script into test my project The code is that : Imports System.Windows.Forms Public Class MilesBox Inherits System.Windows.Forms.UserControl...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.