Renamed functions/methods?

Asked by nbecker

How can I add a method, and rename it?

I'd like to add a python method "__call__" which wraps the c++ method "operator()".

Also, How about if that c++ method is a template?

Question information

Language:
English Edit question
Status:
Solved
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Solved by:
Gustavo Carneiro
Solved:
Last query:
Last reply:
Revision history for this message
Best Gustavo Carneiro (gjc) said :
#1

For both functions and methods, templated or not, the 'custom_name' parameter lets you specify your own name in Python side for the function or method. Example (from tests):

    ## test overloaded functions
    mod.add_function('get_int_from_string', ReturnValue.new('int'),
                     [Parameter.new('char*', 'from_string', is_const=True),
                      Parameter.new('int', 'multiplier', default_value='1')], custom_name="get_int")
    mod.add_function('get_int_from_float', ReturnValue.new('int'),
                     [Parameter.new('double', 'from_float'),
                      Parameter.new('int', 'multiplier', default_value='1')],
                     custom_name="get_int")

However, I'm afraid __call__ will not work in PBG 0.9. Python extension types use 'type slots' for certain special methods. Whilst in Python you define __call__, in C you have to define the tp_call slot, and that requires code generator support. I just added support for this through the Python API in trunk. However, gccxml scanning support isn't done yet.

Revision history for this message
nbecker (ndbecker2) said :
#2

Thanks Gustavo Carneiro, that solved my question.