implicit conversion

John Lenton john at grulic.org.ar
Tue Feb 1 10:20:58 EST 2005


On Mon, Jan 31, 2005 at 02:01:56PM +0100, Benjamin Schmeling wrote:
> Hi,
> 
> I am working on exposing a bigint class to python.  Now I've got the 
> problem that methods which take an bigint as
> an parameter do not accept Python longs.
> 
> For example:
> import _PythonLiDIA
> x=123L;
> c=_PythonLiDIA.bigint();
> _PythonLiDIA.gcd(c,x);
> 
> 
> Traceback (most recent call last):
> File "test.py", line 132, in ?
>  _PythonLiDIA.gcd(a,x);
> Boost.Python.ArgumentError: Python argument types in
>  _PythonLiDIA.gcd(bigint, long)
> did not match C++ signature:
>  gcd(LiDIA::bigint, LiDIA::bigint)
> 
> I don't know how to achieve implicit conversion at this point, turning an
> long automatically into an bigint. The other way round, turning an bigint
> into long can be realized by defining __long__.
> 
> Can someone help me please?

as later on you say you have a constructor to convert longs into your
bigints, you should add a __coerce__ method to your class.

General description of __coerce__:

  http://docs.python.org/ref/numeric-types.html#l2h-303

details on coercion rules:

  http://docs.python.org/ref/coercion-rules.html

enjoy.

-- 
John Lenton (john at grulic.org.ar) -- Random fortune:
BOFH excuse #309:

firewall needs cooling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050201/5fe16dbf/attachment.sig>


More information about the Python-list mailing list