0

I'm just starting to learn Python and I have the following problem. Using a package with method "bind", the following code works:

def callback(data):
    print data
channel.bind(callback)

but when I try to wrap this inside a class:

class myclass:

    def callback(data):
        print data

    def register_callback:
        channel.bind(self.callback)

the call_back method is never called. I tried both "self.callback" and just "callback". Any ideas?

0

3 Answers 3

1

It is not clear to me how your code works, as (1) you did not post the implementation of channel.bind, and (2) your second example is incorrect in the definition of register_callback (it is using a self argument that is not part of the list of parameters of the method, and it lacks parentheses).

Nevertheless, remember that methods usually require a "self" parameter, which is implicitly passed every time you run self.function(), as this is converted internally to a function call with self as its first parameter: function(self, ...). Since your callback has just one argument data, this is probably the problem.

You cannot declare a method bind that is able to accept either a function or a class method (the same problem happens with every OOP language I know: C++, Pascal...).

There are many ways to do this, but, again, without a self-contained example that can be compiled, it is difficult to give suggestions.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass the self object as well:

def register_callback(self):
    channel.bind(self.callback)

Comments

0

What you're doing is entirely possible, but I'm not sure exactly what your issue is, because your sample code as posted is not even syntactically valid. (The second method has no argument list whatsoever.)

Regardless, you might find the following sample code helpful:

def send_data(callback):
    callback('my_data')


def callback(data):
    print 'Free function callback called with data:', data

# The follwing prints "Free function callback called with data: my_data"
send_data(callback)


class ClassWithCallback(object):
    def callback(self, data):
        print 'Object method callback called with data:', data

    def apply_callback(self):
        send_data(self.callback)

# The following prints "Object method callback called with data: my_data"
ClassWithCallback().apply_callback()

# Indeed, the following does the same
send_data(ClassWithCallback().callback)

In Python it is possible to use free functions (callback in the example above) or bound methods (self.callback in the example above) in more or less the same situations, at least for simple tasks like the one you've outlined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.