简体   繁体   English

什么是参考窃取和借用?

[英]What is reference stealing and borrowing ?

While reading the documentation for the python C/API there are terms referring to reference stealing and borrowing.在阅读 python C/API 的文档时,有一些术语指的是参考窃取和借用。 What do they mean ?他们的意思是什么 ?

As user2864740 commented, those terms are defined in the Reference Count Details section of the "Introduction" page for the Python C API docs.正如 user2864740 评论的那样,这些术语在 Python C API 文档的“介绍”页面的引用计数详细信息部分中定义。 Here are the relevant paragraphs:以下是相关段落:

The reference count behavior of functions in the Python/C API is best explained in terms of ownership of references. Python/C API 中函数的引用计数行为最好根据引用的所有权来解释。 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. “拥有引用”意味着在不再需要引用时负责调用 Py_DECREF。 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).所有权也可以转移,这意味着获得引用所有权的代码然后负责在不再需要时通过调用 Py_DECREF() 或 Py_XDECREF() 来最终对其进行解引用——或者将此责任传递给它的调用者)。 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.窃取引用意味着当您将引用传递给函数时,该函数假定它现在拥有该引用,并且您不再对其负责。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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