Adding a field to a 'foreign' object from the outside

Steve Holden steve at
Fri Jan 23 14:26:40 CET 2009

atleta wrote:
>   Hi,
> 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, 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. Which one is the
> 'pythonic' way? 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?
Or there's a third option. Write a class that implements a __call__()
method, and use an instance of that class as your callback. Then the
data can be stored as the callback object's private instance data.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC    

More information about the Python-list mailing list