[Python-Dev] Will the new type system allow automatic coercion?

Guido van Rossum guido@digicool.com
Wed, 04 Jul 2001 21:06:43 -0400


> Something I've suddenly started to need is automatic coercion implemented
> by the source type. I have a type implemented in C, and while
> automatic coercion from any other type to my type is easy to implement
> (in your O& routine you simply check whether the passed object is of a
> type you can coerce) there is no way to do the reverse (at least, not
> that I'm aware of, please enlighten me).
> 
> And now I have this CFString type (a wrapper around the MacOS
> CoreFoundation object. Nice things, by the way, these CoreFoundation
> objects, sort-of inherited from NextStep and they share a lot of
> design with Python objects, but I digress) that can show itself as a
> Unicode string or an 8 bit string or a number of other things. It
> would be nice if users could pass these CFString objects in places
> where a string or unicode is expected. Simply said, if PyArg_Parse s
> format would accept my objects.
> 
> Will the new type system allow me to do this?

I don't know that the new type system (which isn't really a type
system, just a generalized implementation of class construction
through a formalization of the Don Beaudry hook :-) has anything to do
with this, but can't the buffer interface come to the rescue?  The s
format accepts anything that conforms to the buffer interface, AFAIK.

Does that help?  (Alas, I don't think there's a similar API for
Unicode.)

--Guido van Rossum (home page: http://www.python.org/~guido/)