Python方法只能工作一次

时间:2014-11-29 03:35:47

标签: python methods scope

我正在编写一种计算2到8个时间序列变量的协方差的方法。我打算在将变量传递给此方法时将变量包含在列表对象中。该方法应返回1个数字,而不是协方差矩阵。

该方法在第一次调用时工作正常。在此之后调用它时,它返回0.示例附加在我的代码下方的底部。关于可变范围问题的任何建议/反馈都将非常感激。谢谢!

p = [3,4,4,654]
o = [4,67,4,1]

class Toolkit():
    def CovarianceScalar(self, column1, column2 = [], column3 = [], column4 = [],column5 = [],column6  = [],column7  = [],column8 = []):
    """Assumes all columns have length equal to Len(column1)"""
    #If only the first column is passed, this will act as a variance function
    import numpy as npObject
    #This is a binary-style number that is assigned a value of 1 if one of the input vectors/lists has zero length.  This way, the CovarianceResult variable can be computed, and the relevant
    # terms can have a 1 added to them if they would otherwise go to 0, preventing the CovarianceResult value from incorrectly going to 0.
    binUnityFlag2 = 1 if (len(column2) == 0) else 0
    binUnityFlag3 = 1 if (len(column3) == 0) else 0
    binUnityFlag4 = 1 if (len(column4) == 0) else 0
    binUnityFlag5 = 1 if (len(column5) == 0) else 0
    binUnityFlag6 = 1 if (len(column6) == 0) else 0
    binUnityFlag7 = 1 if (len(column7) == 0) else 0
    binUnityFlag8 = 1 if (len(column8) == 0) else 0

    # Some initial housekeeping:  ensure that all input column lengths match that of the first column.  (Will later advise the user if they do not.)
    lngExpectedColumnLength = len(column1)
    inputList = [column2, column3, column4, column5, column6, column7, column8]
    inputListNames =  ["column2","column3","column4","column5","column6","column7","column8"]

    for i in range(0,len(inputList)):
        while len(inputList[i]) < lngExpectedColumnLength:   #Empty inputs now become vectors of 1's.
            inputList[i].append(1)

    #Now start calculating the covariance of the inputs:
    avgColumn1 = sum(column1)/len(column1)                  #<-- Each column's average 
    avgColumn2 = sum(column2)/len(column2)
    avgColumn3 = sum(column3)/len(column3)
    avgColumn4 = sum(column4)/len(column4)
    avgColumn5 = sum(column5)/len(column5)
    avgColumn6 = sum(column6)/len(column6)
    avgColumn7 = sum(column7)/len(column7)
    avgColumn8 = sum(column8)/len(column8)
    avgList = [avgColumn1,avgColumn2,avgColumn3,avgColumn4,avgColumn5, avgColumn6, avgColumn7,avgColumn8]
    #start building the scalar-valued result:
    CovarianceResult = float(0)
    for i in range(0,lngExpectedColumnLength):
        CovarianceResult +=((column1[i] - avgColumn1) * ((column2[i] - avgColumn2)  + binUnityFlag2) *  ((column3[i] - avgColumn3)  + binUnityFlag3 )   * ((column4[i] - avgColumn4)  + binUnityFlag4 )  *((column5[i] - avgColumn5)  + binUnityFlag5)  * ((column6[i] - avgColumn6)  + binUnityFlag6 )  *  ((column7[i] - avgColumn7)  + binUnityFlag7)* ((column8[i] - avgColumn8)  + binUnityFlag8))

    #Finally, divide the sum of the multiplied deviations by the sample size:
    CovarianceResult = float(CovarianceResult)/float(lngExpectedColumnLength)       #Coerce both terms to a float-type to prevent return of array-type objects. 
    return CovarianceResult

示例:

myInst = Toolkit()    #Create a class instance.

首次执行该功能:

 myInst.CovarianceScalar(o,p)    
 #Returns -2921.25, the covariance of the numbers in lists o and p.

Second time around:

myInst.CovarianceScalar(o,p)    
#Returns: 0.0

1 个答案:

答案 0 :(得分:5)

我相信你面临的问题是mutable default个论点。基本上,当您第一次执行myInst.CovarianceScalar(o,p)时,除前两列之外的所有列都是[]。在执行期间,您可以更改参数。因此,当您执行与之前相同的函数myInst.CovarianceScalar(o,p)时,参数中的其他列不再是[]。它们获取第一次执行后所拥有的任何值的值。