[C++-sig] Static, again...

Nicodemus nicodemus at globalite.com.br
Thu Dec 5 05:30:29 CET 2002


Scott A. Smith wrote:

>David wrote:
>
>>A static member function is just a regular function; you can add it at
>>module scope:
>>
>>BOOST_PYTHON_MODULE_INIT(numTest)
>>{
>>   def("getDims", &Num::getDims);
>>   ...
>>}
>>
>
>Yep, this compiles just fine, but that is not the problem. 
>Unfortunately, I cannot call it from Python without errors.
>Here is what happens when I added the code to the hello example
>(Cygwin/GCC BPV2):
>
>$ python
>Python 2.2.2 (#1, Nov 15 2002, 07:49:04)
>[GCC 2.95.3-5 (cygwin special)] on cygwin
>Type "help", "copyright", "credits" or "license" for more information.
>
>>>>from hello import *
>>>>X = Num()
>>>>print X.dims()
>>>>
>5
>
>>>>print X.getDims()
>>>>
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>TypeError: bad argument type for built-in operation
>
>>>>print Num().getDims()
>>>>
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>TypeError: bad argument type for built-in operation
>
>>>>print Num.getDims()
>>>>
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>TypeError: unbound method Boost.Python.function object must
>be called with Num instance as first argument (got nothing instead)
>

You are exposing a function, not a member function, so you should call 
it like this from python:

 >>> print getDims()

If that bothers you, you can:

1) Export it as "Num_getDims" instead of "getDims". It might be acceptable.


2) Create a static function on the python side, like this:

 >>> from hello import *
 >>> Num.getDims = staticmethod(getDims)
 >>> print Num.getDims()


3) Wait for David to implement the new facility to export static 
functions, which he said he would do as soon as he could, IIRC.

>Thanks, Scott
>

Best Regards,
Nicodemus.







More information about the Cplusplus-sig mailing list