[Python-ideas] For CPython 3.1: make type Check and CheckExact macros consistent? universal?
larry at hastings.org
Fri Apr 24 15:55:04 CEST 2009
There are a bunch of type-checking macros in Include/*.h. Most classes
only provide one, Check(), implemented as follows:
#define PyWhatever_Check(ob) (Py_TYPE(ob) == &PyWhatever_Type)
Examples of this style include PyRange, PyMemoryView, PyFunction, and
Then there are types with a more sophisticated view of type identity.
For example, an object can qualify as a list even if its type is not
PyList_Type. For such types, there's a Check() macro that does a more
sophisticated check. They'll also have a CheckExact() macro doing an
exact type check; that'll look like PyWhatever_Check above.
Then there's PyMethod. PyMethod has a CheckExact macro but *doesn't*
have a Check macro. Hmm!
When I stumbled across that it got me to thinking. Might it be a good
idea for all Python types to have both Check and CheckExact macros? The
CheckExact form would be consistent, and for types that only need an
"exact" check they could define Check in terms of CheckExact:
#define PyWhatever_CheckExact(ob) (Py_TYPE(ob) == &PyWhatever_Type)
#define PyWhatever_Check PyWhatever_CheckExact
This would let you express your intent. If you were expecting a
Whatever-like object, you always use Check. If you require that exact
object, you always use CheckExact.
On the other hand, maybe every type-checking macro that does
(Py_TYPE(ob) == &PyWhatever_Type) should be called CheckExact, and for
types that don't have an inexact check they don't have a Check.
Or maybe the existing subclass-style Check macros should be renamed
CheckSubclass (or Implemented or something) and Check should always do a
I admit this isn't a stupendous idea, but I thought it was worth typing
up. If there's interest I'd be happy to supply a patch for whichever
form found favor.
Or we could forget the whole thing, you and I,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas