[Pyrex] callback

Bryan Weingarten bryan.weingarten at pobox.com
Tue Aug 19 08:10:15 CEST 2003


> > my problem is that the i need user data to be passed into
status_callback
> > which gets passed back to the python callback.
>
> No, you don't. You just need to pass a suitable callable
> object, such as a bound method or nested function which
> has access to the required data, as the callback function.
>
> Greg Ewing, Computer Science Dept,
+--------------------------------------+
> University of Canterbury,    | A citizen of NewZealandCorp, a   |
> Christchurch, New Zealand    | wholly-owned subsidiary of USA Inc.  |
> greg at cosc.canterbury.ac.nz    +--------------------------------------+
>


greg,

thank you for this obviously correct way to solve this problem.  i was too
bogged down trying to stay true to the c version i'm wrapping that i lost
what is the more pythonic way to solve this.  your suggestion of using a
nested function worked just fine.  but i can't get the bound method way to
work.

-- nested status function works

class Myplay:
    def play(self, mp3file):
        def status(sec, msec):
            print sec, msec, self.data
            return 1

        self.data = ('test', 2.0, {1:'one'})
        m = xxx.Play(mp3file)
        m.status_callback(status)

if __name__ == '__main__':
    m = Myplay()
    m.play(sys.argv[1])


-- bound status method does not work
 i'm not sure what the line
m.status_callback(self.status)
should be.  i also tried Myplay.status.  and having status have/have not the
self parameter. the c code crash in all cases.  are you sure it's possible
to pass in a bound method?


class Myplay:
    def status(self, sec, msec):
        print sec, msec, self.data
        return 1

    def play(self, mp3file):
        self.data = ('test', 2.0, {1:'one'})
        m = xxx.Play(mp3file)
        m.status_callback(self.status)

if __name__ == '__main__':
    m = Myplay()
    m.play(sys.argv[1])


thanks,

bryan





More information about the Pyrex mailing list