Problem with Property

G

Guest

* none said:
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
--
extension?
Feature. -- gefunden in de.org.ccc
 
P

Peter Hansen

none <"@bag.python.org said:
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
 
S

Steve Holden

none <"@bag.python.org said:
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
 
F

Felipe Almeida Lessa

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"
 
N

none

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
 
N

none

André Malo said:
* 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
 
N

none

André Malo said:
* 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
 
N

none

Steve said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top