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

Discussion in 'Python' started by atleta, Jan 23, 2009.

  1. atleta

    atleta Guest

    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?

    Thanks,
    Laszlo
     
    atleta, Jan 23, 2009
    #1
    1. Advertising

  2. atleta

    James Mills Guest

    On Fri, Jan 23, 2009 at 1:19 PM, atleta <> wrote:
    > 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?


    The more elegant approach is your 2nd one.
    A wrapper object that hold the extra data and functionality
    you require while maintaining the APIs needs.

    Tacking things on is a bit hackish :) (but nonetheless probably works).

    --JamesMills
     
    James Mills, Jan 23, 2009
    #2
    1. Advertising

  3. atleta schrieb:
    > 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?


    For me, the first one. Creatin a mapping is cumbersome, opens up all
    kinds of questions like "how to remove stale entries" and is a level of
    indirection that one has to be aware of.

    The session object is obviously intended to be that - a storage for
    information persistent over the course of the actions working with it.
    So - use it.

    Python is dynamic. Use that dynamicity. The java & C++-guys would do it
    - if they could.

    Diez
     
    Diez B. Roggisch, Jan 23, 2009
    #3
  4. atleta a écrit :
    > 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,


    Why so ? And what does your API documentation says about this session
    object?

    > 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.
     
    Bruno Desthuilliers, Jan 23, 2009
    #4
  5. atleta

    Steve Holden Guest

    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.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 23, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H5N1
    Replies:
    0
    Views:
    470
  2. Michael Turner
    Replies:
    1
    Views:
    419
    Konstantinos Pantos
    Mar 13, 2007
  3. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    313
    Victor Bazarov
    Feb 7, 2012
  4. Sound
    Replies:
    2
    Views:
    497
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    491
Loading...

Share This Page