Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70693
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Subject | Re: Help with changes in traceback stack from Python 2.7 to Python 3.x |
| Date | 2014-04-28 15:35 -0400 |
| References | <CAPTjJmooUJa8uPx6JvO-HnO2q8T8cR_yDWciSpZHVWenx6O7hw@mail.gmail.com> <06134705-e2b1-4a2f-adb6-bec4faa96e83@me.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9557.1398713770.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Help with changes in traceback stack from Python 2.7 to Python 3.x Ned Batchelder <ned@nedbatchelder.com> - 2014-04-28 15:35 -0400
csiph-web