[C++-sig] Re: accessing the python type system
David Abrahams
dave at boost-consulting.com
Wed May 28 17:04:56 CEST 2003
Stefan Seefeld <seefeld at sympatico.ca> writes:
> David Abrahams wrote:
>> Stefan Seefeld <stefan.seefeld at orthosoft.ca> writes:
>>
>>>hi there,
>>>
>>>are there any functions/operators in boost.python to
>>>let me access the python type system from within C++ ?
>>>
>>>For example holding two python objects I'd like to test
>>>whether one's type is derived from the other's type,
>>>whether they are derived from a given type, i.e. things
>>>like 'type', 'isinstance', etc.
>> These are more functions in the same category as len(). See my reply
>> to Philippe Fremy today on this list.
>
> What about a 'type object' similar to 'type_info', i.e. with
> a 'name()' accessor and comparison operators ?
Well, if you have isinstance you don't need that.
In principle, there's no reason we shouldn't have something like you
propose, only it should be a wrapper around Python's 'type' just the
same way that boost::python::dict is a wrapper around Python's
'dict'...
...but once you start go go down that road you get in trouble. Python
makes type(x) return a reference to x.__class__ (unless x is a
"classic" instance, in which case it returns "instance"). Because
type(type(x)) also has to work (get x's metatype), it can't have a
regular copy constructor. When copies of an object are not equivalent
to the original in C++ it plays havoc.
I guess 'type' would have to be an instance of a class derived from
boost::python::object, whose unary function call operator returns its
argument's type. I wouldn't want a 'name()' function because Python
type objects don't have that:
>>> class X(object): pass
...
>>> X.name()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object 'X' has no attribute 'name'
>>> X.__name__
'X'
The equivalent in C++ would be X.attr("__name__")
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list