I guess I am missing something big as I am looking for a shorthand way of doing
the following:
dctA = dict(x=x, y=y, ... n=n)
This is, as I understand it a very natural way of using a dictionary. It seems that this syntax is unnecessarily redundant and hence my goal of writing something more compact. Perhaps the way I am doing it is a little unorthodox, but the ultimate use of a dictionary is, as I understand it, completely in line with how dictionaries were designed to be used. In my other code, I often use these dictionaries to pass arguments to functions and return results. It allows me great flexibility without breaking existing code. I pack a dictionary before passing and unpack when retrieving.
I will give the locals approach a try, it seems a little more clumsy than
simply passing the variables to the function.
Thanks again for your input.
---Andrew
On Apr 27, 2014, at 01:18 PM, Chris Angelico <ros...@gmail.com> wrote:
On Mon, Apr 28, 2014 at 4:56 AM, Andrew Konstantaras <akon...@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
--
https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list