Help with changes in traceback stack from Python 2.7 to Python 3.x

Chris Angelico rosuav at gmail.com
Sun Apr 27 16:18:16 EDT 2014


On Mon, Apr 28, 2014 at 4:56 AM, Andrew Konstantaras <akonsta at icloud.com> wrote:
> Thanks for the response and I can certainly see that this old code can be
> improved, but I respectfully disagree on the utility of this function.  The
> flexibility of passing any number of arguments to the function and returning
> a dictionary is much easier than writing out dict(x=x, y=y, ...n=n).  I also
> believe that "makeDict" makes my code very readable.

There is another option. You could pass a set of strings and a
dictionary of all locals, and have it fetch them. However, I
personally disagree about the readability; yes, it's nice and clean,
but it does something which 99% of Python programmers won't expect.
It's like writing a numeric type in C++ and then overloading the +
operator to concatenate the decimal representations.

> My question is around finding the names of the variables passed to a
> function from within the function.  I have spent many hours looking on the
> web looking for where in the frames and stacks this information is stored.
> Does anyone have any insight where/how I can find the variables associated
> with the arguments in
>
>       dctA = makeDict(strA, intA)
>
> from within the function
>
>       def makeDict(*args):
>             .... #need to find the variable names "strA" and "intA" in this
> context

It isn't stored. What would be the names provided if, for instance, you do this:

dctA = makeDict("literal", intA + intB)

? There is no name for a literal or an expression. One of the
principles of Python is that an object is itself, regardless of the
name used to reference it. You're violating that by peeking into the
source code; you're fundamentally going against what every Python
programmer will expect.

Please don't do this. Take a step back, see what problem you're really
trying to solve, and solve that problem another way. Maybe a simple
DSL will help here, or possibly even just string interpolation,
depending on what you're trying to do. If you need help, show us some
of the code that uses makeDict, and we can advise.

Just please don't inflict makeDict onto any subsequent maintainer.

ChrisA



More information about the Python-list mailing list