[Tutor] Class property: check dict values before assign
runsun
runsun@bilbo.bio.purdue.edu
Thu Nov 14 14:14:10 2002
hi all,
I have a class having a property of type dictionary. I wanna
check the values before they are assigned to that dictionary
property.
The __setattr__ seems to be the candidate for this, but no.
It is not even called when the user sets a dictionary value.
See the following example:
class myclass:
def __init__(self):
self.__dict__['user']={'Iastname':'lee', 'firstname':'john'}
def __setattr__(self, name, value):
print 'Calling _setattr__ ....'
c = myclass()
c.user = {'Iastname':'lee', 'firstname':'john'} # ===> [A]
c.user['lastname'] = 'dilley' # ===> [B]
The line [A] called the __setattr__, but line [B] didn't. Therefore,
it seems to be impossible for python to verify the content of a
dictionary-typr properties, unless all the key:values of that
dictionary are given all together at the same time.
Any idea ??
pan
----------------------------------------------------------------------
~~ Be like water, be shapeless ~~
Runsun Pan, PhD Ecology & Evolution, U of Chicago
TEL: 773-834-3965 EMAIL: pan@uchicago.edu
FAX: [USA]775-659-8969 [Taiwan] 094-656-8001
----------------------------------------------------------------------
]