|
Re: accessing the python type system: msg#00309python.c++
Stefan Seefeld <seefeld@xxxxxxxxxxxx> writes: > David Abrahams wrote: >> Stefan Seefeld <stefan.seefeld@xxxxxxxxxxxx> 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 |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: registering simple conversions from/to python: 00309, David Abrahams |
|---|---|
| Next by Date: | Re: Re: accessing the python type system: 00309, Stefan Seefeld |
| Previous by Thread: | Re: Re: accessing the python type systemi: 00309, Stefan Seefeld |
| Next by Thread: | Re: Re: accessing the python type system: 00309, Stefan Seefeld |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |