property puzzle

G

george young

[python 2.3.3, x86 linux, part of a substantial gtk/postgres app]
I want a Run instance to have a Status instance which inherits
from Observable. I want to be able to say:

thisrun.status = 'planned'

and have thisrun's _set_status do side-effects and then the status
object's setter incorporate the new value as it's new state.
But the setter functions never seem to get called.
Am I using "property" wrong?

class Observable: #("observer" software pattern)
def __init__(self):
self.observers = []
def register(self, func, *args, **kw):
self.observers.append((func, args, kw))
def notify(self):
for func, args, kw in self.observers: func(*args, **kw)

class RunStatus(Observable):
def __init__(self, val):
Observable.__init__(self)
self._value = val
def set(self, newval):
print 'RunStatus.set'
self._value = newval
self.notify()
def get(self):
print 'RunStatus.get'
return self._value

class Run:
def __init__(self):
self._status = RunStatus('planned')
def _get_status(self):
return self._status
def _set_status(self, val):
# do various things with val...
self._status.set(val)
status = property(_get_status, _set_status)

if __name__ == '__main__':
def stat_display():
print 'display to screen: r.status=', r.status
r = Run()
r.status.register(stat_display) #many registers in real app.
r.status='hold'
print 'status=', r.status
print r._status.observers

Status needs to be a real and persistant object so it's
observers persist, but needs it's value changed simply, like
r.status = 'something'

[flames about using string values for what is semantically a
six valued enum *are* welcome in a separate thread...]
 
R

Russell Blau

george young said:
[python 2.3.3, x86 linux, part of a substantial gtk/postgres app]
I want a Run instance to have a Status instance which inherits
from Observable. I want to be able to say:

thisrun.status = 'planned'

and have thisrun's _set_status do side-effects and then the status
object's setter incorporate the new value as it's new state.
But the setter functions never seem to get called.
Am I using "property" wrong?

class Observable: #("observer" software pattern)

'property' only works with new-style classes. Try changing to

class Observable(object):

and also make the same change in class Run.
 
J

Jean Brouwers

Also, property needs bound methods to get, set, and delete, like

status = property(self._get_status, self._set_status)

/Jean Brouwers
 
S

Shalabh Chaturvedi

Jean said:
Also, property needs bound methods to get, set, and delete, like

status = property(self._get_status, self._set_status)

/Jean Brouwers

No it doesn't. The following is a working piece of code:

class C(object):
def getx(self):
return 1

x = property(getx)

print C().x

Prints 1 as expected.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top