when does python delete variables?

前端 未结 4 1590
借酒劲吻你
借酒劲吻你 2020-12-14 22:15

I know that python has an automatic garbage collector and so it should automatically delete variables when there are no more reference to them.

My impression is tha

4条回答
  •  暖寄归人
    2020-12-14 23:00

    Write stuff you want to clear from memory in separate functions. In your example, you can do

      def xdef(z):
        x = f(z) # x is a np.array and contains a lot of data 
        x0 = x[0]
    
      def funz(z):
        xdef(z)
        y = f(z + 1) # y is a np.array and contains a lot of data
        y0 = y[0]  
    
        return y[0], x[0] 
    

    This will cause an exception

提交回复
热议问题