Håvard Vegge <hava...@stud.ntnu.no> writes:

Hi Håvard, good to hear from you!

> I have a program with two classes (as outlined below). From the
> class MyFunctions I would like to call a function in the class
> Protocol, is this possible? Normally one would save the instance of
> a class p = Protocol() and then be able to call p.some_function()...

Right, that is the correct way to invoke a method on an object.

> But I don't see how this is possible because the Protocol is started
> as a callback. Does anyone have some suggestions?
> Best regards, Håvard
> class MyFunctions:
>    def initiate_computation(self, code):
>       // want to call Protocol.some_function(code)

You must then have access to the Protocol object. Let's pass that as
another parameter:

    def initiate_computation(self, protocol, code):

and then do


> class Protocol:
>    def __init__(self, runtime):
>       ...

Somewhere -- maybe here -- you must invoke the initiate_computation
method in MyFunctions. Otherwise it cannot wish to call the
Protocol.some_function method.

If __init__ is that place, then do it like this:

  myfunc = MyFunctions()

  myfunc.initiate_computation(self, "code...")

Here self is a reference to the Protocol object, exactly what
initiate_computation needs in the MyFunctions class.

By the way, if MyFunctions only contain functions, then you might not
need a class at all. Python is much more free-form than, say, Java in
this way -- in Java you must put "static" functions into a class,
whereas you can just put them in a module in Python.

I hope that makes some sense, otherwise please ask again :-)

Martin Geisler
viff-devel mailing list (http://viff.dk/)

Reply via email to