Adding a field to a 'foreign' object from the outside
bruno.42.desthuilliers at websiteburo.invalid
Fri Jan 23 10:05:31 CET 2009
atleta a écrit :
> I'm working with a callback API (a DBus one) and I'd need to store
> some state between the calls somewhere. I know that it's possible to
> extend an object with fields after creation, so I could just store my
> data in the session object that is passed in with every callback.
> However it stinks from OO perspective,
Why so ? And what does your API documentation says about this session
> at least to me. The other
> option, something that I'd do in java for example, would be to set up
> a mapping (a dict) using the session object as a key.
Is this object _safely_ usable as a key somehow ? And if yes, do you
have a way to deal with stale sessions ?
> Which one is the
> 'pythonic' way?
The simplest. But as far as I'm concerned, I don't have enough context
to say which one it would be here.
> Do you use the first method (modifying an object that
> you don't even control the source of) or do you write a bit more code
> and go with the second one?
Depends - cf above. But as a guideline, unless the library provides a
defined way to solve this problem or the library documentation clearly
states I should *not* store my own data in the session object (but then
what's the point of a session object ???), I'd just use it.
More information about the Python-list