加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 什么是参考窃取和借用?

发布时间:2020-12-20 11:35:26 所属栏目:Python 来源:网络整理
导读:在阅读 python C / API的文档时,有一些术语涉及引用窃取和借用.他们的意思是什么 ? 解决方法 正如user2864740所评论的那样,这些术语在 Python C API文档的“简介”页面的 Reference Count Details section中定义.以下是相关段落: The reference count beha
在阅读 python C / API的文档时,有一些术语涉及引用窃取和借用.他们的意思是什么 ?

解决方法

正如user2864740所评论的那样,这些术语在 Python C API文档的“简介”页面的 Reference Count Details section中定义.以下是相关段落:

The reference count behavior of functions in the Python/C API is best explained in terms of ownership of references. Ownership pertains to references,never to objects (objects are not owned: they are always shared). “Owning a reference” means being responsible for calling Py_DECREF on it when the reference is no longer needed. Ownership can also be transferred,meaning that the code that receives ownership of the reference then becomes responsible for eventually decref’ing it by calling Py_DECREF() or Py_XDECREF() when it’s no longer needed—or passing on this responsibility (usually to its caller). When a function passes ownership of a reference on to its caller,the caller is said to receive a new reference. When no ownership is transferred,the caller is said to borrow the reference. Nothing needs to be done for a borrowed reference.

Conversely,when a calling function passes in a reference to an object,there are two possibilities: the function steals a reference to the object,or it does not. Stealing a reference means that when you pass a reference to a function,that function assumes that it now owns that reference,and you are not responsible for it any longer.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读