Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect when a class member is updated

0 views
Skip to first unread message

Boethius

unread,
Jan 15, 2003, 9:34:50 AM1/15/03
to
Can I detect when a class member is added or updated?

I have a class with a member called path. Everytime it's updated, I'd
like to run a 'sanity check' on the new value.

How can I do this?

Steve Holden

unread,
Jan 15, 2003, 9:42:49 AM1/15/03
to
"Boethius" <boet...@techie.com> wrote in message
news:67446a0a.0301...@posting.google.com...

Take a look at "setattr()" generally, and the __setattr__() special method
inparticular. This allows you to trap access to attributes.

regards
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon! http://www.python.org/pycon/

Tomasz Lisowski

unread,
Jan 15, 2003, 9:46:05 AM1/15/03
to
Uzytkownik "Boethius" <boet...@techie.com> napisal w wiadomosci
news:67446a0a.0301...@posting.google.com...

> Can I detect when a class member is added or updated?
>
> I have a class with a member called path. Everytime it's updated, I'd
> like to run a 'sanity check' on the new value.
>

Declare a method, that will update the member, and perform the sanity check.
The member itself could be made private (leading underscores), so that the
only method accessing the member is the one, that performs also the check.

Tomasz Lisowski


Gerhard Häring

unread,
Jan 15, 2003, 9:50:54 AM1/15/03
to
Steve Holden <sho...@holdenweb.com> wrote:
> "Boethius" <boet...@techie.com> wrote in message
> news:67446a0a.0301...@posting.google.com...
>> Can I detect when a class member is added or updated?
>>
>> I have a class with a member called path. Everytime it's updated, I'd
>> like to run a 'sanity check' on the new value.
>>
>> How can I do this?
>
> Take a look at "setattr()" generally, and the __setattr__() special method
> inparticular. This allows you to trap access to attributes.

That was necessary before properties were introduced in Python 2.2. So you
might as well write something like:

#v+
class MyClass(object):
def __init__(self):
self._path = None

def _get_path(self):
return self._path

def _set_path(self, value):
if isinstance(value, str): # a primitive check
self._path = value
else:
raise ValueError

path = property(_get_path, _set_path)
#v-

Note that properties only work with new-style classes (inherited from
object). See http://python.org/2.2.2/descrintro.html for details.

Gerhard
--
Gerhard Häring
OPUS GmbH München
Tel.: +49 89 - 889 49 7 - 32
http://www.opus-gmbh.net/

Jørgen Cederberg

unread,
Jan 15, 2003, 9:53:45 AM1/15/03
to

How about using properties, as this example shows. You could replace the
notify method with a validate method instead.

class Detect(object):
__value = 0
def set_value(self, value):
self.__value = value
self.notify()
def get_value(self):
return self.__value
def notify(self):
print "Value was changed to", str(self.value)
value = property(get_value, set_value)

Regards
Jorgen

Peter Hansen

unread,
Jan 15, 2003, 12:29:56 PM1/15/03
to

Minor nit-picking on the terminology: you probably mean you
have an *object* (an instance of a class) with a "member"
(or attribute) which you want to sanity check.

Note that classes are also objects in Python, and those too
can be updated on-the-fly, but you don't appear to want to
do that.

-Peter

Boethius

unread,
Jan 15, 2003, 4:21:19 PM1/15/03
to
Gerhard Häring <gerhard...@opus-gmbh.net> wrote in message news:<slrnb2at1n.1hk....@haering.opus-gmbh.net>...

> Note that properties only work with new-style classes (inherited from
> object). See http://python.org/2.2.2/descrintro.html for details.

Thanks, this properties thing was totally unknown to me.

Mike C. Fletcher

unread,
Jan 15, 2003, 4:49:48 PM1/15/03
to
Properties are, IMO, the great unsung feature of 2.2.x that should have
_everyone_ move to it (the feature that everyone sings about, the
type-class unification, is undeniably wonderful too, but everyone
already sings about that). Subclassed properties give you amazing power
in automating data-driven Python applications. They can:

* interact with watchers, (update databases or GUIs on set/del, for
example)
* check data-types,
* perform automatic value coercian (ingoing and outgoing),
* linearise their own definitions (i.e. they can write themselves
out to disk and be reloaded later to facilitate run-time-composed
data-types (classes)),
* provide attribute documentation (and other attributes/properties)
to automate presentation,
* automate object initialisers,
* and provide default values/functions.

(Among other uses).

Hey, how did I get up on this box?
Mike

Boethius wrote:

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Bengt Richter

unread,
Jan 15, 2003, 8:23:22 PM1/15/03
to
On Wed, 15 Jan 2003 16:49:48 -0500, "Mike C. Fletcher" <mcfl...@rogers.com> wrote:

>Properties are, IMO, the great unsung feature of 2.2.x that should have
>_everyone_ move to it (the feature that everyone sings about, the
>type-class unification, is undeniably wonderful too, but everyone
>already sings about that). Subclassed properties give you amazing power
>in automating data-driven Python applications. They can:
>
> * interact with watchers, (update databases or GUIs on set/del, for
> example)
> * check data-types,
> * perform automatic value coercian (ingoing and outgoing),
> * linearise their own definitions (i.e. they can write themselves
> out to disk and be reloaded later to facilitate run-time-composed
> data-types (classes)),
> * provide attribute documentation (and other attributes/properties)
> to automate presentation,
> * automate object initialisers,
> * and provide default values/functions.
>
>(Among other uses).
>

If you look at Delphi, you might wonder when events will become
elements in the Python language too ;-)

(Which BTW, IMO is worth putting off long enough to get right ;-)

Regards,
Bengt Richter

0 new messages