Re: OrderedDict with kwds

2017-04-22 Thread Ben Finney
Albert-Jan Roskam  writes:

> The basic problem is that kwds is a regular, unordered dict […]

(Albert, you are probably aware that the above passage is not what you
wrote. But your message shows it indistinguishable from your other text.

Please teach your email client to compose quoted material using
 the
conventional line prefix of “> ” — and, if your email client can't be
taught to do that in plain text, please choose a better email client.)

> Yes, I realized this later that evening (probably thanks to can of
> cold beer :-)). But there is hope:
> https://www.python.org/dev/peps/pep-0468/ . Do you know if there
> is/will be a "from __future__" to backport that behavior? We're using
> Python 3.5 now.

The ‘__future__’ features are language features, and like other features
they do not get added retro-actively to an already released Python
version. So, Python 3.5 has a set of ‘__future__’ features that are the
only ones that will ever be in Python 3.5.

When a feature is added to ‘__future__’ it is to introduce a
backward-incompatible feature gradually, and like any feature is always
added as part of the development of Python versions. So, new features in
‘__future__’ will only appear in *not yet released* Python versions.



In short: To get the changed behaviour, you need a newer Python version.

-- 
 \ “If you don't want your beliefs to be ridiculed, don't have |
  `\such ridiculous beliefs.” —Greta Christina, 2011-10-22 |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread Ben Finney
INADA Naoki  writes:

> On Sat, Apr 22, 2017 at 10:41 PM, Ben Finney  
> wrote:
> > So, I would recommend continuing to code as though ‘dict’ is not
> > ordered, at least until a Python version is released with a clear
> > statement that ordering can be relied upon.
>
> While dict's order is implementation detail, keyword is ordered by
> language spec.

Ah, I had missed that distinction. Thank you for the explanation.

Yes, Python 3.6 keyword arguments now preserve the order from the
function call. Great!

-- 
 \   “If you always want the latest and greatest, then you have to |
  `\  buy a new iPod at least once a year.” —Steve Jobs, MSNBC |
_o__) interview 2006-05-25 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread INADA Naoki
On Sat, Apr 22, 2017 at 10:41 PM, Ben Finney  wrote:
> INADA Naoki  writes:
>
>> From Python 3.6, keyword arguments are ordered. So the docstring is
>> outdated.
>
> (Thank you, Inada-san, for the implementation!)
>
> The announcement of the change specifies that we should not rely on
> ordered ‘dict’:
>
> The order-preserving aspect of this new implementation is considered
> an implementation detail and should not be relied upon […].
>
> 
> 
>
> So, I would recommend continuing to code as though ‘dict’ is not
> ordered, at least until a Python version is released with a clear
> statement that ordering can be relied upon.

See https://www.python.org/downloads/release/python-360/

> PEP 468 Preserving Keyword Argument Order

While dict's order is implementation detail, keyword is ordered by
language spec.
You can rely on it on Python 3.6+.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread Albert-Jan Roskam
From: eryk sun <eryk...@gmail.com>
Sent: Saturday, April 22, 2017 7:59 AM
To: Python Main
Cc: Albert-Jan Roskam
Subject: Re: OrderedDict with kwds
    
On Fri, Apr 21, 2017 at 6:08 PM, Albert-Jan Roskam
<sjeik_ap...@hotmail.com> wrote:
> Would the insertion order be preserved if the last line were to be
> replaced with:
>
> if kwds:
> for k, v in kwds.items():
> self[k] = v
> if args:
> self.__update(*args)  # no **kwds!

The basic problem is that kwds is a regular, unordered dict:

    def f(**kwds):
    print(type(kwds))

    >>> f()
    


> Hi Eryk,

Yes, I realized this later that evening (probably thanks to can of cold beer 
:-)). But there is hope: https://www.python.org/dev/peps/pep-0468/ . Do you 
know if there is/will be a "from __future__" to backport that behavior? We're 
using Python 3.5 now.

Thank you!

Albert-Jan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread Ben Finney
INADA Naoki  writes:

> From Python 3.6, keyword arguments are ordered. So the docstring is
> outdated.

(Thank you, Inada-san, for the implementation!)

The announcement of the change specifies that we should not rely on
ordered ‘dict’:

The order-preserving aspect of this new implementation is considered
an implementation detail and should not be relied upon […].



So, I would recommend continuing to code as though ‘dict’ is not
ordered, at least until a Python version is released with a clear
statement that ordering can be relied upon.

-- 
 \   “Always do right. This will gratify some people, and astonish |
  `\the rest.” —Mark Twain |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread Peter Otten
Albert-Jan Roskam wrote:

> For regular dicts I like to use the dict() function because the code is
> easier to write and read. But OrderedDict() is not equivalent to dict():
> In the docstring of collections.OrderedDict it says "keyword arguments are
> not recommended because their insertion order is arbitrary"
> (https://github.com/python/cpython/blob/3.6/Lib/collections/__init__.py)
> 
> It took me while to realize that. What is the best way to use keywords to
> create an ordered dict, while maintaining insertion order?

That's the equivalent to "How can I eat my cake and have it." Once you pass 
keyword arguments order is inevitably lost

$ python3 -c 'f = lambda **kw: list(kw); print(f(a=1, b=2))'
['a', 'b']
$ python3 -c 'f = lambda **kw: list(kw); print(f(a=1, b=2))'
['a', 'b']
$ python3 -c 'f = lambda **kw: list(kw); print(f(a=1, b=2))'
['b', 'a']

in all Python versions prior to 3.6. 

However, in 3.6 dict keys stay in insertion order, so you don't even need an 
OrderedDict anymore. Specifically

https://docs.python.org/dev/whatsnew/3.6.html

"""
CPython implementation improvements:
...
The order of elements in **kwargs now corresponds to the order in which 
keyword arguments were passed to the function.
"""


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread INADA Naoki
On Sat, Apr 22, 2017 at 3:08 AM, Albert-Jan Roskam
 wrote:
> For regular dicts I like to use the dict() function because the code is 
> easier to write and read. But OrderedDict() is not equivalent to dict():
> In the docstring of collections.OrderedDict it says "keyword arguments are 
> not recommended because their insertion order is arbitrary" 
> (https://github.com/python/cpython/blob/3.6/Lib/collections/__init__.py)
>

>From Python 3.6, keyword arguments are ordered.  So the docstring is outdated.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OrderedDict with kwds

2017-04-22 Thread eryk sun
On Fri, Apr 21, 2017 at 6:08 PM, Albert-Jan Roskam
 wrote:
> Would the insertion order be preserved if the last line were to be
> replaced with:
>
> if kwds:
> for k, v in kwds.items():
> self[k] = v
> if args:
> self.__update(*args)  # no **kwds!

The basic problem is that kwds is a regular, unordered dict:

def f(**kwds):
print(type(kwds))

>>> f()

-- 
https://mail.python.org/mailman/listinfo/python-list