471,350 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

detecting property modification

Howdy,

I think it is easier to explain my question with a short example:

class Body:
def __init__(self, pos):
self.__dict__['pos'] = pos

def __setattr__(self, name, value):
if name == 'pos':
print 'pos changed to', value

self.__dict__[name] = value
>>b = Body([0, 0])
b.pos = [3, 4]
pos changed to [3, 4]
b.pos[0] = 5 # How do I detect this in the Body?

I would like to print 'pos changed to [5, 4]' when pos is modified
with direct access to the pos object (i.e. b.pos[0] = 5)
My first instinct is to modify the Body's __dict__ by using
metaclasses.
I don't have any experience with meta classes but from what I remember
this should be possible but I thought there could be a simpler way to
do this.

What is the Pythonic way to do this? Can anybody provide a short
example for either case (with or without metaclasses).

Regards
Dec 21 '07 #1
0 733

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alessandro Crugnola *sephiroth* | last post: by
reply views Thread by Joăo Santa Bárbara | last post: by
25 posts views Thread by Ryan Stewart | last post: by
6 posts views Thread by mccoyn | last post: by
1 post views Thread by Paul W | last post: by
4 posts views Thread by wardy | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.