dictionary as property
Gerhard Haering
gh at ghaering.de
Tue Jul 19 13:00:10 EDT 2005
On Tue, Jul 19, 2005 at 07:56:20PM +0300, Thanos Tsouanas wrote:
> Hello.
>
> (How) can I have a class property d, such that d['foo'] = 'bar' will run
> a certain function of the class with 'foo' and 'bar' as it's arguments?
You could implement a custom container type that will do what you want.
See http://docs.python.org/ref/sequence-types.html for __setitem__.
Quick hack:
>>> class Foo:
... def __init__(self):
... self.d = self
... def __setitem__(self, key, value):
... getattr(self, key)(value)
... def bar(self, param):
... print "bar got called with", param
...
>>> foo = Foo()
>>> foo.d["bar"] = 42
bar got called with 42
>>>
-- Gerhard
--
Gerhard Häring - gh at ghaering.de - Python, web & database development
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050719/46189401/attachment.sig>
More information about the Python-list
mailing list