By the way, this is already in the CPython source code if I remember from
when I worked on 448. The code for dict unpacking is merely blocked. I
like this syntax from a purity standpoint, but I don't think I would use it
that much.
On Thursday, June 8, 2017 at 3:18:21 PM UTC-4, Nick Badger
a few notes:
>From the OP:
It would be cool to have a syntax that would unpack the dictionary to
> values based on the names of the variables. Something perhaps like:
>
> a, b, c = **mydict
>
> which would assign the values of the keys 'a', 'b', 'c' to the variables.
>
a number of alternatives
I ha d not read all the e-mails in the thread - but just to announce
to people eager for a similar feature -
I have a package on pypi that enables one to do:
In [74]: with extradict.MapGetter({'a': 1}) as d:
...: from d import a
...:
In [75]: a
Out[75]: 1
(just pip install extradict )
While I don't like that syntax, we do know that sets are unhashable, so we
can be certain that that would be a TypeError if it was meant to construct
a set containing a set. (ie. {{foo}} will always result in a TypeError in
python).
On Thu, Jun 8, 2017 at 1:40 PM Chris Angelico
On Fri, Jun 9, 2017 at 6:02 AM, MRAB wrote:
> It could also be used on the RHS to pack:
>
a = 1
b = 2
c = 3
d = 4
foo = {{a, b, c, d}}
The trouble with that is that it's syntactically identical to creating
a set containing a set containing
On 2017-06-08 20:16, Nick Badger wrote:
Well, it's not deliberately not destructive, but I'd be more in favor of
dict unpacking-assignment if it were spelled more like this:
>>> foo = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> {'a': bar, 'b': baz, **rest} = foo
>>> bar
1
>>>
Well, it's not deliberately not destructive, but I'd be more in favor of
dict unpacking-assignment if it were spelled more like this:
>>> foo = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> {'a': bar, 'b': baz, **rest} = foo
>>> bar
1
>>> baz
2
>>> rest
{'c': 3, 'd': 4}
On 8 June 2017 at 17:49, Paul Moore wrote:
> On 8 June 2017 at 08:15, Stephen J. Turnbull
> wrote:
>> If you like this feature, and wish it were in Python, I genuinely wish
>> you good luck getting it in. My point is just that in
On Jun 8, 2017 1:35 AM, "Greg Ewing" wrote:
C Anthony Risinger wrote:
> Incredibly useful and intuitive, and for me again, way more generally
> applicable than iterable unpacking. Maps are ubiquitous.
>
Maps with a known, fixed set of keys are relatively uncommon
Lucas Wiman writes:
> > Maps with a known, fixed set of keys are relatively uncommon
> > in Python, though.
>
> This is false in interacting with HTTP services, where frequently you're
> working with deserialized JSON dictionaries you expect to be in a precise
> format (and fail if not).
Hi Lucas,
I would consider converting the dict into a namedtuple then.
Essentially the namedtuple acts as a specification for expected fielsds:
abc = namedtuple("ABC", "a b c")
d = {"a":1, "b": 2, "c":3} # presumably you got this from reading some JSON
abc(**d)
# returns: ABC(a=1, b=2, c=3)
>
> Maps with a known, fixed set of keys are relatively uncommon
> in Python, though.
This is false in interacting with HTTP services, where frequently you're
working with deserialized JSON dictionaries you expect to be in a precise
format (and fail if not).
On Wed, Jun 7, 2017 at 11:32 PM,
C Anthony Risinger wrote:
Incredibly useful and
intuitive, and for me again, way more generally applicable than iterable
unpacking. Maps are ubiquitous.
Maps with a known, fixed set of keys are relatively uncommon
in Python, though. Such an object is more likely to be an
object with named
One existing way to do this:
a, b, c = (mydict[k] for k in ('a', 'b', 'c'))
--
Greg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
> In python 3.6+ this is better since the dictionary is insertion-ordered,
but is still not really what one would probably want.
Be careful: ordered dict is an implementation detail. You must use
explicitly collections.OrderedDict() to avoid bad surprises.
In CPython 3.7, dict might change
Thank you! This overview really helps!
On Thu, Jun 08, 2017 at 11:18:06AM +1000, Steven D'Aprano
wrote:
> On Wed, Jun 07, 2017 at 06:14:08PM +, Nick Humrich wrote:
>
> > It would be cool to have a syntax that would unpack the dictionary to
> > values based on the names
On Wed, Jun 07, 2017 at 06:14:08PM +, Nick Humrich wrote:
> It would be cool to have a syntax that would unpack the dictionary to
> values based on the names of the variables. Something perhaps like:
>
> a, b, c = **mydict
This was discussed (briefly, to very little interest) in March/April
On Jun 7, 2017 5:54 PM, "Erik" wrote:
On 07/06/17 23:42, C Anthony Risinger wrote:
> Neither of these are really comparable to destructuring.
>
No, but they are comparable to the OP's suggested new built-in method
(without requiring each mapping type - not just dicts
On Jun 7, 2017 5:42 PM, "C Anthony Risinger" wrote:
On Jun 7, 2017 5:15 PM, "Matt Gilson" wrote:
On Wed, Jun 7, 2017 at 3:11 PM, Erik wrote:
> On 07/06/17 19:14, Nick Humrich wrote:
>
>> a, b, c = mydict.unpack('a',
On 07/06/17 23:42, C Anthony Risinger wrote:
Neither of these are really comparable to destructuring.
No, but they are comparable to the OP's suggested new built-in method
(without requiring each mapping type - not just dicts - to implement
it). That was what _I_ was responding to.
E.
On Wed, Jun 7, 2017 at 3:11 PM, Erik wrote:
> On 07/06/17 19:14, Nick Humrich wrote:
>
>> a, b, c = mydict.unpack('a', 'b', 'c')
>>
>
> def retrieve(mapping, *keys):
>return (mapping[key] for key in keys)
>
>
>
Or even:
from operator import itemgetter
retrieve =
On 07/06/17 19:14, Nick Humrich wrote:
a, b, c = mydict.unpack('a', 'b', 'c')
def retrieve(mapping, *keys):
return (mapping[key] for key in keys)
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for
22 matches
Mail list logo