how to know who calls a method

Discussion in 'Python' started by Alex Garel, Apr 9, 2004.

  1. Alex Garel

    Alex Garel Guest

    Hello,
    Maybe it's a strange question.
    I heard that salltalk use messages that are sent from one object to another.
    It seems in other OO languages, method call is quite an implementation
    of that. The only thing is, the receiver (instance which method is being
    called) can't retrieve reference of the sender. Is it possible in Python
    ? Is there a standard way or a workaround.
    I think about using frame stack (in module inspect) but it only gives me
    the name of the function from which I was called, not a reference to the
    object.
    Why am I asking for such a feature ?
    It is because I would like an object to be aware of another object
    reading it so it can signal future change in its value to the last. I
    can use a getter which precise who is the reader but my attempt was to
    be able to use a normal getter (x=y) in a Phytonic fashion !
    Alex Garel, Apr 9, 2004
    #1
    1. Advertising

  2. On Fri, 09 Apr 2004 23:54:02 +0200,
    Alex Garel <> wrote:

    > It is because I would like an object to be aware of another
    > object reading it so it can signal future change in its value to
    > the last. I can use a getter which precise who is the reader but
    > my attempt was to be able to use a normal getter (x=y) in a
    > Phytonic fashion !


    The usual solution involves some sort of "registration" (untested
    and definitely needs more details and error checking, etc.):

    class emitter:

    def __init__( self ):
    self.collectors = [ ]
    self.data = None

    def register_for_updates( self, collector ):
    self.collectors.append( collector )

    def set_data( self, newdata ):
    self.data = newdata
    for collector in self.collectors:
    collector( self, newdata )

    class collector:

    def __init__( self, watch_this_object ):
    watch_this_object.register_for_updates( self.collect_updates )

    def collect_updates( self, other_object, new_data ):
    print other_object, "just got new data:", new_data

    Without this sort of set up, emitter.set_data wouldn't know
    exactly what to do when there's new data anyway. This approach is
    more flexible than a fixed method invocation should you have a
    many-to-many emitter-to-collector relationship.

    There's also no reason you couldn't wrap emitter.set_data up in
    some sort of property so it would work as part of the getter
    and/or setter for some attribute (but I don't like properties:
    explicit is better than implicit).

    HTH,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
    Heather Coppersmith, Apr 10, 2004
    #2
    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. Honne Gowda A
    Replies:
    2
    Views:
    856
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    746
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Richard Tobin
    Replies:
    24
    Views:
    767
  4. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    216
    Gregory Toomey
    Apr 23, 2004
  5. Bob
    Replies:
    5
    Views:
    246
Loading...

Share This Page