writing to locals (was RE: execfile: NameError exception thrown for things in locals())

Fredrik Lundh fredrik at pythonware.com
Mon Apr 9 16:13:14 EDT 2001


James_Althoff at i2.com wrote:
> ... my class ...
>
>     def setFormula(self,formulaString):
>      self.formula = formulaString
>
>     def getCalculatedValueAt(self,rowIndex):
>         count = self.getColumnCount()  # excludes the calculated column
>         localsDict = locals()
>         for columnIndex in xrange(count):
>             columnID = 'c' + str(columnIndex+1)
>             columnValue = self.getValueAt(rowIndex,columnIndex)
>             localsDict[columnID] = columnValue
>         try:
>             value = eval(self.formula)
>         except:
>             value = 'Invalid formula'
>         return value

         localsDict = {}
         for columnIndex in xrange(count):
             columnID = 'c' + str(columnIndex+1)
             columnValue = self.getValueAt(rowIndex,columnIndex)
             localsDict[columnID] = columnValue
         try:
             value = eval(self.formula, localsDict)
         except:
             value = 'Invalid formula'
         return value

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->







More information about the Python-list mailing list