Py_INCREF/DECREF:何时 [英] Py_INCREF/DECREF: When

查看:456
本文介绍了Py_INCREF/DECREF:何时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在陈述以下内容时是正确的:

Is one correct in stating the following:

  1. 如果在C函数中创建了Python对象,但该函数未返回该对象,则不需要INCREF,但需要DECREF.

[false]如果函数确实返回了它,那么您确实需要在接收返回值的函数中INCREF.[/false]

[false]If the function does return it, you do need to INCREF, in the function that receives the return value.[/false]

将C类型的变量作为属性(如doubleint等)分配给Python对象时,不需要INCREFDECREF.

When assigning C typed variables as attributes, like double, int etc., to the Python object, no INCREF or DECREF is needed.

以下使用安全吗?将Python对象作为属性分配给其他Python对象的过程如下:

Is the following safe to use? Assigning Python objects as attributes to your other Python objects goes like this:

PyObject *foo;
foo = bar;  // A Python object
tmp = self->foo;
Py_INCREF(foo);
self->foo = foo;
Py_XDECREF(tmp);
// taken from the manual, but it is unclear if this works in every situation

  • 对于其作为属性具有的其他所有Python对象,都需要DECREF取消对Python对象的重新分配,但对于C类型的属性则不需要.

  • Deallocation of a Python object needs to DECREF for every other Python object that it has as an attribute, but not for attributes that are C types.


    关于将C类型作为属性",我的意思是bar和baz:

    With regards to 'C type as an attribute', I mean bar and baz:

    typedef struct {
        PyObject_HEAD
        PyObject *foo;
        int bar;
        double baz;
    } FooBarBaz;
    

    推荐答案

    首先,请仔细阅读,尤其是最后一段

    First, read this more carefully, specifically the last paragraph, http://docs.python.org/extending/extending.html#ownership-rules

    考虑它的简便方法是考虑引用计数.

    Easy way to think about it is thinking about the reference counts.

    1. 您的第一个陈述是正确的.如果创建一个新的Python对象(例如PyLong),则它的引用计数已经为1.如果您要返回它,那么很好,但是如果您不打算返回它,那么它必须是垃圾

    1. Your first statement is correct. If you create a new Python object (say PyLong) then it already has a reference count of 1. This is fine if you're going to return it but if you're not going to return it, it needs to be garbage collected by Python and it is only marked for GC with refcount=0, thus you need to DECREF if you're not going to return it.

    第二条语句为假.如果您需要返回它并创建它,则只需将其返回即可.归还所有权.如果要在返回前使用INCREF,则是要告诉Python 也保留了一个副本.同样,如果创建它,则refcount = 1.如果然后执行INCREF,则refcount = 2.但这不是您想要的,您想以refcount = 1返回.

    The second statement is false. If you need to return it and you created it, just return it. Returning transfers ownership. If you were to INCREF before returning, then you're telling Python that you also are retaining a copy. So again, if you create it, refcount=1. If you then do INCREF then refcount=2. But this is not what you want, you want to return with refcount=1.

    我不确定是否 知道这个,但这更多是与C相关的问题.您如何将intdouble添加到Python对象?

    I'm not quite sure I get this but this is more of a C related question. How are you adding an int or double to a Python object?

    您能举个例子说明该方法行不通吗?

    Can you give an example where that method won't work?

    同样,我不确定C类型是Python对象的属性.每个intdoublelong等都由Python对象以某种方式包装.

    Again, I'm not sure when a C type is an attribute of a Python object. Every int, double, long, etc. is wrapped by a Python object in some way or another.

    以上链接概述了这些答案的注意事项.阅读完之后,您甚至根本不需要我的拙劣解释.我希望我澄清了,不要再混淆了.

    The caveats to these answers are outlined in the link above. You really shouldn't even need my poor explanation after reading that. I hope I clarified and didn't confuse more.

    这篇关于Py_INCREF/DECREF:何时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆