Python to C and Garbage Collector -


i have following problem in python (problem variable ref or val in c)

my c code:

#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)     (int isize = 0; isize < 1000; isize++)        pxtab->pdtab[isize] = dvalue;      return pxtab;  } 

sample of python code:

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         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 use scalar function status of myobject equal allocated when enter in anothercfunction see status of object list non_allocated because python garbage collector has been called due scalar(mylist[i]) local called

could please me rewrite python code in order python not called garbage collector on list[i] ? thanks

i still don't think there garbage collection problems code you've posted far. there other significant problems python code. (fwiw, c code looks ok, suppose have been using c while still new python). biggest problem definition of myclass faulty. haven't shown code create instance of myclass , how use it, there may additional problems.

when class method called gets passed class instance first argument, conventionally named self. argument not specified in args given when call method must specified in 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 add() method defined 3 args, when call supply 2 args explicitly, since first arg, self, implicit.

most of method definitions not have self first arg, apart __del__ method. python doesn't care name arguments, shove self whatever first argument in method's definition. methods (apart __del__) getting called wrong args.

also, when call method inside method of class need indicate that, prefixing method's name self.. eg

def transformvalue(self, mylist):     #the rest of method definition     #...  def myfunc(self, mylist):     listvalue = self.transformvalue(mylist)     return anothercfunction(listvalue) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -