How do I set a callback in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • catsclaw

    How do I set a callback in Python?

    I can't for the life of me figure out how to set a callback in
    Python. I have a class, which wraps another class. The second class
    needs a callback assigned. I don't want to use globals for it.
    Here's what I'd like to do:

    class MyWrapper:
    def get_login(self, username):
    return self.user, self.pass

    def __init__(self, user, pass):
    self.user = user
    self.pass = pass

    self.client = Client("connect ion string")
    self.client.cal lback_login = get_login

    .... but obviously, the Client class, when it calls the callback,
    doesn't pass a reference to the "self" object. How do I do this?

    -- Chris
  • Rob Williscroft

    #2
    Re: How do I set a callback in Python?

    catsclaw wrote in news:d797403a-e492-403f-933a-bd18ef53d5c0
    @k13g2000hse.go oglegroups.com in comp.lang.pytho n:
    I can't for the life of me figure out how to set a callback in
    Python. I have a class, which wraps another class. The second class
    needs a callback assigned. I don't want to use globals for it.
    Here's what I'd like to do:
    >
    class MyWrapper:
    def get_login(self, username):
    return self.user, self.pass
    >
    def __init__(self, user, pass):
    self.user = user
    self.pass = pass
    >
    self.client = Client("connect ion string")
    self.client.cal lback_login = get_login
    >
    ... but obviously, the Client class, when it calls the callback,
    doesn't pass a reference to the "self" object. How do I do this?

    use:
    self.client.cal lback_login = self.get_login


    Rob.
    --

    Comment

    • Diez B. Roggisch

      #3
      Re: How do I set a callback in Python?

      catsclaw schrieb:
      I can't for the life of me figure out how to set a callback in
      Python. I have a class, which wraps another class. The second class
      needs a callback assigned. I don't want to use globals for it.
      Here's what I'd like to do:
      >
      class MyWrapper:
      def get_login(self, username):
      return self.user, self.pass
      >
      def __init__(self, user, pass):
      self.user = user
      self.pass = pass
      >
      self.client = Client("connect ion string")
      self.client.cal lback_login = get_login
      >
      ... but obviously, the Client class, when it calls the callback,
      doesn't pass a reference to the "self" object. How do I do this?

      Do self.get_login. The difference is that this creates a so-called
      "bound method". Google for that, and play around in the interpreter with
      an object and references to it's methods, either through the class or
      the instance to see the difference.

      Diez

      Comment

      • Gary Herron

        #4
        Re: How do I set a callback in Python?

        catsclaw wrote:
        I can't for the life of me figure out how to set a callback in
        Python. I have a class, which wraps another class. The second class
        needs a callback assigned. I don't want to use globals for it.
        Here's what I'd like to do:
        >
        class MyWrapper:
        def get_login(self, username):
        return self.user, self.pass
        >
        def __init__(self, user, pass):
        self.user = user
        self.pass = pass
        >
        self.client = Client("connect ion string")
        self.client.cal lback_login = get_login
        >
        Make that list line:

        self.client.cal lback_login = self.get_login


        Then you are passing a "bound" method (meaning bound to a particular instance).
        The client class calls its callback_login as a normal function with parameters,
        and the Wrapper get_login will be called on the proper instance of MyWrapper.

        This is a very common thing for many uses, including callbacks.


        Hoping I understood what you wanted correctly,
        Gary Herron



        ... but obviously, the Client class, when it calls the callback,
        doesn't pass a reference to the "self" object. How do I do this?
        >
        -- Chris
        --

        >

        Comment

        Working...