[C++-sig] tuple, list, dict, et. al

David Abrahams david.abrahams at rcn.com
Wed Jun 26 18:48:10 CEST 2002


From: "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com>


> --- David Abrahams <david.abrahams at rcn.com> wrote:
> > Answer 1:
> >
> >     The function only matches if the actual argument is a Python
> >     tuple or a subclass of tuple
> >
> > Answer 2:
> >
> >     The function matches if the actual argument is any Python sequence;
> >     tuple(arg) will be applied to retrieve the tuple object used inside
> >     the wrapped function
>
> And here is the third opinion: give the user a choice. If you want a
tuple and
> only a tuple, the user specifies boost::python::tuple as the argument. If
any
> sequence is acceptable, boost::python::sequence_fast, which is a wrapper
around
> the PySequence_Fast family of Python API functions (just like the one in
the
> bpl_utils that I posted before).

That's actually answer 1, which is also what I was leaning towards.
However, note that PySequence_Fast is not a complete solution, in that it
only works with tuples and lists. To see the difference:

    >>> xrange(10)        # not a tuple or list
    xrange(10)
    >>> tuple(xrange(10)) # converts any sequence
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

The version of Ralf's sequence conversion code that I accept into
Boost.Python should really work for any sequence, IMO. So, while I don't
mind having a boost::python::sequence_fast which matches tuple and list
arguments, I really want a boost::python::sequence type which matches any
sequence.

-Dave







More information about the Cplusplus-sig mailing list