您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

`is`运算符是否在Python中使用__magic__方法?

`is`运算符是否在Python中使用__magic__方法?

不,is是直接指针比较,id只是将转换为的对象地址返回long

来自ceval.c

case PyCmp_IS:
    res = (v == w);
    break;
case PyCmp_IS_NOT:
    res = (v != w);
    break;

vw这里简单PyObject *

来自bltinmodule.c

static PyObject *
builtin_id(PyObject *self, PyObject *v)
{
    return PyLong_FromVoidPtr(v);
}

PyDoc_STRVAR(id_doc,
"id(object) -> integer\n\
\n\
Return the identity of an object. This is guaranteed to be unique among\n\
simultaneously existing objects. (Hint: it's the object's memory address.)");
python 2022/1/1 18:38:52 有260人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶