On Dec 8, 2006, at 17:07, carlos choy wrote:

I looked at the Demo folder and found usage of the Callable interface in

rangetext.cxx.

In version 5.36, rangetest.cxx has the following lines (starting at line

29):


*******************************************

    RangeSequence r2(1, 10, 2);

    if(r2[1] != Py::Int(3))

        return "RangeSequence check failed. ";


    debug_check_ref_queue();


    // calling an extension object method using getattr

    Py::Callable w(r2.getAttr("amethod"));

    Py::Tuple args(1);

    Py::Int j(3);

    args[0]=j;

    Py::List answer(w.apply(args));

    if(answer[0] != r2)

        return ("Extension object test failed (1)");


    if(answer[1] != args[0])

        return ("Extension object test failed (2)");

*******************************************


I see that RangeSequence is a class extending a python sequence.  However, I

don't see how "amethod" could be an attribute of it.  "amethod" is a method

of the range class, but this class seems to have nothing to do with

RangeSequence.  How is this method an attribute of the extended class?



RangeSequence is Py::Object that points to an instance of range. See line 112 of range.hxx.


Next, I see that 'w' is a Callable object, but I do not see how it can have

an 'apply' method.  Is the 'apply' method putting the arguments in 'args'

into the method "amethod"?


It works the same way that apply builtin works in python. Call the function that w contains
passing it args and store the returned value in answer.


Lastly, I don't see at all how the Callable object is calling back into a

python script.


The apply() functions calls into python.


Can you please give me some explanations?  Sorry if this all seems rather

elementary, but I am trying my best to understand it.



No problem.


Barry