python项目中的奇怪bug

2024-04-27 03:47:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用Python做一个大项目,遇到了一个奇怪的错误,我无法解释。在我的一个类中,有一个私有方法在实例化期间被调用:

def _convertIndex(self, dimInd, dimName):
    '''Private function that converts numbers to numbers and non-integers using 
    the subclass\'s convertIndex.'''
    print dimInd, ' is dimInd'
    try:
        return int(dimName)
    except:
        if dimName == '*':
            return 0
        else:
            print self.param.sets, ' is self.param.sets'
            print type(self.param.sets), ' is the type of self.param.sets'
            print self.param.sets[dimInd], ' is the param at dimind'
            return self.param.sets[dimInd].value(dimName)

打印内容:

0  is dimInd
[<coremcs.SymbolicSet.SymbolicSet object at 0x10618ad90>]  is self.param.sets
<type 'list'>  is the type of self.param.sets
<SymbolicSet BAZ=['baz1', 'baz2', 'baz3', 'baz4']>  is the param at dimind
======================================================================
ERROR: testParameterSet (GtapTest.TestGtapParameter)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/myuser/Documents/workspace/ilucmc/gtapmcs/test/GtapTest.py", line 116, in testParameterSet
pset = ParameterSet(prmFile, dstFile, GtapParameter)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/ParameterSet.py", line 103, in __init__
self.distroDict, corrDefs = AbsBaseDistro.readFile(distFile, self.paramDict)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 359, in readFile
distro = cls.parseDistro(param, target, distroType, args)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 301, in parseDistro
return cls(param, target, distro, dim_names, argDict)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 150, in __init__
self.dim_indxs = list(starmap(self._convertIndex, enumerate(dim_names)))  # convert to numeric values and save in dim_indxs
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 194, in _convertIndex
print self.param.sets[dimInd], ' is the param at dimind'
IndexError: list index out of range

显然,这不是整个类的代码,但它代表了一些我不明白的东西。当我索引到自参数集. 很明显,迪明在射程之外。问题是,dimInd是0自参数集是一个长度为1的列表(如print语句所示),为什么我不能索引到它?你知道吗

EDIT:不管它值多少钱,__init__方法如下所示:

'''
Stores a definitions of a distribution to be applied to a header variable.
See the file setup/gtap/DistroDoc.txt for the details.
'''
def __init__(self, param, target, distType, dim_names, argDict):
    self.name = param.name
    self.dim_names = dim_names
    self.dim_indxs = []
    self.target = target.lower() if target else None
    self.distType = distType.lower() if distType else None
    self.rv = None
    self.argDict = {}
    self.modifier = defaultdict(lambda: None)
    self.param = param

    # Separate args into modifiers and distribution arguments
    for k, v in argDict.iteritems():
        if k[0] == '_':  # modifiers start with underscore
            self.modifier[k] = v
        else:
            self.argDict[k] = v  # distribution arguments do not have underscore

    if self.target == 'index':
        print dim_names
        self.dim_indxs = list(starmap(self._convertIndex, enumerate(dim_names)))  # convert to numeric values and save in dim_indxs

    if distType == 'discrete':
        entries = self.modifier['_entries']
        if not entries:
            raise DistributionSpecError("Not enough arguments given to discrete distribution.")
        modDict = {k[1:]: float(v) for k, v in self.modifier.iteritems() if k[1:] in getOptionalArgs(DiscreteDist.__init__)}
        self.rv = DiscreteDist(entries, **modDict)
        return
    sig = DistroGen.signature(distType, self.argDict.keys())
    gen = DistroGen.generator(sig)

    if gen is None:
        raise DistributionSpecError("Unknown distribution signature %s" % str(sig))

    self.rv = gen.makeRV(self.argDict)  # generate a frozen RV with the specified arguments
    self.isFactor = gen.isFactor

Tags: thetoinselftargetifparamnames