简体   繁体   English

从Python到C和垃圾收集器

[英]Python to C and Garbage Collector

I have the following problem in Python (problem with variable by ref or val in C) 我在Python中遇到以下问题(C中ref或val的变量问题)

My C Code: 我的C代码:

#define NON_ALLOCATED 0
#define ALLOCATED 1
#define MAX_STACK 100

typedef struct myObject
{
   int iStatus;
   int iSize;
   double pdTab;
}

static myObject globaTab[MAX_STACK]

myObject *AllocateATab(int iSize)
{
   myObject *pxTab = NULL;
   int i;
   int iFound = 0;
   while(i < MAX_STACK && !iFound)
   {
      if (globaTab[i].iStatus = NON_ALLOCATED)
         iFound = 1;
         break;
   }
   if (i < MAX_STACK)
   {
      pxTab = globaTab + i;
      pxTab->iStatus = ALLOCATED;
      pxTab->pdTab = (double *)calloc(iSize, sizeof(double));
   }
   return pxTab;
}

int FreeATab(myObject *pxTab)
{
    if (pxTab)
    {
        pxTab->iStatus = NON_ALLOCATED;
        if (pxTab->pdTab)
           free(pxTab->pdTab);

    }
}

myObject *Scalar(double dValue)
{
    myObject *pxTab = AllocateATab(1000)
    for (int iSize = 0; iSize < 1000; iSize++)
       pxTab->pdTab[iSize] = dValue;

    return pxTab;

}

sample of my Python Code: 我的Python代码示例:

class MyClass(object):)
   def __del__(self):
      mylibC.FreeATab(self)

   def Scalar(valueinput):
      return mylibC.Scalar(valueinput)

   def transformValue(mylist):
        Len=len(mylist)
        res = [0]*Len
        for i in xrange(Len):
          if(type(mylist[i]) == float):
            res[i] = Scalar(mylist[i])
          else:
            res[i] = mylist[i]
        return res

    def myFunc(mylistlist):
        listValue = transformValue(mylist)
        return anotherCFunction(listValue)

When I use Scalar function status of myObject is equal to Allocated However when I enter in anotherCFunction I see that status of object List is now to NON_ALLOCATED because python garbage collector has been called due to Scalar(mylist[i]) is a local called 当我使用Scalar函数时,myObject的状态等于已分配。但是当我输入anotherCFunction时,我看到对象列表的状态现在变为NON_ALLOCATED,因为由于Scalar(mylist [i])被调用了python垃圾收集器,

Could you please help me to rewrite this python code in order that Python does not called garbage collector on List[i] ? 您能否帮助我重写此python代码,以便Python不会在List [i]上调用垃圾回收器? Thanks 谢谢

I still don't think that there are garbage collection problems with the code you've posted so far. 仍然认为到目前为止您发布的代码没有垃圾回收问题。 But there are other significant problems with your Python code. 但是您的Python代码还有其他重大问题。 (FWIW, the C code looks ok, I suppose you have been using C for a while but are still fairly new to Python). (FWIW,C代码看起来还不错,我想您已经使用C了一段时间了,但是对于Python来说还是相当陌生的)。 The biggest problem is that the definition of MyClass is faulty. 最大的问题是MyClass的定义是错误的。 You haven't shown us the code where you create an instance of MyClass and how you use it, so there may be additional problems. 您尚未向我们展示创建MyClass实例的代码以及如何使用它,因此可能还会有其他问题。

When a class method is called it gets passed the class instance as the first argument, conventionally named self . 调用类方法时,它将类实例作为第一个参数传递,通常称为self This argument is not specified in the args given when you call the method but it must be specified in the method definition. 调用该方法时,未在给出的参数中指定此参数,但必须在方法定义中指定该参数。

Eg, 例如,

class SimpleClass(object):
    def add(self, x, y):
        return x + y

simple = SimpleClass()
print simple.add_one(2, 3)  

output 产量

5

So the add() method is defined with three args, but when we call it we only supply two args explicitly, since the first arg, self , is implicit. 因此add()方法定义了三个arg,但是当我们调用它时,我们仅显式提供了两个arg,因为第一个arg self是隐式的。

Most of your method definitions do not have self as their first arg, apart from your __del__ method. 除了__del__方法之外,大多数方法定义都没有self作为第一个参数。 Python doesn't care what you name your arguments, it will shove self into whatever is the first argument in the method's definition. Python不在乎您为参数命名的内容,它会将self推入方法定义中的第一个参数。 So all your methods (apart from __del__ ) are getting called with wrong args. 因此,您所有的方法(除了__del__ )都被错误的args调用。

Also, when you call a method inside another method of that class you need to indicate that, normally by prefixing the method's name with self. 同样,当您在该类的另一个方法中调用一个方法时,通常需要在方法名称前加上self.来表明这一点self. . Eg 例如

def transformValue(self, mylist):
    #The rest of the method definition
    #...

def myFunc(self, mylist):
    listValue = self.transformValue(mylist)
    return anotherCFunction(listValue)

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

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