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

Ned Batchelder ned at nedbatchelder.com
Mon Apr 28 15:35:54 EDT 2014


On 4/27/14 5:51 PM, Andrew Konstantaras wrote:
> 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)
>

Yes, your makeDict(x, y) is a shorthand for dict(x=x, y=y), but there 
are many things you can do with dict that you can't do with makeDict. 
What is the makeDict equivalent of:

     dict(x=12, y=self.y, z=a+b)

The code you have allows you more compact expression, but it brings 
fragility and surprise.

> 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.

Perhaps you want to create a class instead?  If you find yourself 
passing more than a handful of arguments to a function, and especially 
more than a handful of values returned from a function, then a class 
with methods might be a better way to combine state and behavior.

Also, keep in mind that you can return a tuple from a function if you 
want to return two or three values and assign them to names:

     x, y, z = compute_xyz()

You mention unpacking your dictionary after the function call.  How do 
you do that? Isn't that a cumbersome and repetitive operation?

>
> 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

BTW, it's a little easier to follow the threads of conversation if you 
put your responses after the text you are responding to.  This is known 
as bottom-posting, and is preferred to top-posting as you did here.


-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list