<html><body><div><div>I wrote the following code that works in Python 2.7 that takes the
variables passed to the function into a dictionary. The following call:<br><br> strA = 'a'<br> intA = 1<br> dctA = makeDict(strA, intA)<br><br>produces the following dictionary:<br><br> {'strA':'a', 'intA':1}<br><br>To
access the names passed into the function, I had to find the
information by parsing through the stack. The code that used to work
is:<br><br>from traceback import extract_stack<br><br>def makeDict(*args):<br> strAllStack = str(extract_stack())<br> intNumLevels = len(extract_stack())<br> intLevel = 0<br> blnFinished = False<br> while not blnFinished:<br> strStack = str(extract_stack()[intLevel])<br> if strStack.find("makeDict(")>0:<br> blnFinished = True<br> intLevel += 1<br> if intLevel >= intNumLevels:<br> blnFinished = True<br> strStartText = "= makeDict("<br> intLen = len(strStartText)<br> intOpenParenLoc = strStack.find(strStartText)<br> intCloseParenLoc = strStack.find(")", intOpenParenLoc)<br> strArgs = strStack[ intOpenParenLoc+intLen : intCloseParenLoc ].strip()<br> lstVarNames = strArgs.split(",")<br> lstVarNames = [ s.strip() for s in lstVarNames ] <br> if len(lstVarNames) == len(args):<br> tplArgs = map(None, lstVarNames, args)<br> newDict = dict(tplArgs)<br> return newDict<br> else:<br>
return "Error, argument name-value mismatch in function 'makeDict'.
lstVarNames: " + str(lstVarNames) + "\n args: " + str(args), strAllStack<br><br>The
same code does not work in Python 3.3.4. I have tried parsing through
the stack information and frames and I can't find any reference to the
names of the arguments passed to the function. I have tried inspecting
the function and other functions in the standard modules, but I can't
seem to find anything that will provide this information.<br><br>Can anyone point me in the direction to find this information? Any help is appreciated.<br><br>---Andrew<br><br></div></div></body></html>