简体   繁体   中英

Implementing PyMyType_Check methods with Python C API?

All the Python-provided types have a check method (ie, PyList_Check ) that allows you to check if an arbitrary PyObject* is actually a specific type.

How can I implement this for my own types? I haven't found anything good online for this, though it seems like a pretty normal thing to want to do.

Also, maybe I'm just terrible at looking through large source trees, but I cannot for the life of me find the implementation of PyList_Check or any of it's companions in the Python (2.5) source.

That's because they're macros that use deep magic. Save yourself a bit of headache and use PyObject_IsInstance() instead.

If you'd like to implement your own macro, you could try...

#define PyMyType_Check(op) \
    PyObject_TypeCheck(op, &PyMyType_Type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM