Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-15 Thread Mike Miller

On 2018-04-12 18:03, Guido van Rossum wrote:
It's a slippery slope indeed. While having to change update() alone wouldn't 
worry me, the subclass constructors do seem like they are going to want changing 
too, and that's indeed a bit much. So let's back off a bit. Not every three 
lines of code need a built-in shorthand.


This is disappointing since the dictionary is one of the most used but 
simultaneously limited of the builtin types.  It doesn't support a lot of 
operations that strings, lists, tuples, sets, etc do.  These are the little 
niceties that make Python fun to program in.  But, for some reason we're stingy 
when it comes to dictionaries, the foundation of the language.


Has anyone disagreed the dict constructor shouldn't take multiple arguments?

Also, it isn't always three lines of code, but expands with the number that need 
to be merged.


My guess is that the dict is used an order of magnitude more than specialized 
subclasses, even more so now that the Ordered variant is unnecessary in newer 
versions.  It wouldn't bother me at all if it took a few years for the 
improvement to get rolled out to subclasses or never, it's quite a minor 
disappointment compared to getting the functionality ~90% of the time.


Also wouldn't mind helping out with the subclasses if there is some lifting that 
needed to be done.


-Mike
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Serhiy Storchaka

12.04.18 22:42, Andrés Delfino пише:

I think the update method can (and personally, should) stay unchanged:

spam.update(dict(x, y))

seems succinct and elegant enough, with the proposed constructor syntax.

Sorry my ignorance, do (Mutable)Mapping ABC say anything about 
constructors?


Mapping and MutableMapping ABCs don't have constructors, but many 
dict-like objects imitate the dict constructor: accept a single mapping 
or a sequence of pairs as a positional argument, and accept other dict 
as kwargs.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Andrés Delfino
There's a long thread about the subject:
https://mail.python.org/pipermail/python-ideas/2015-February/031748.html

I suggest to avoid the matter altogether :)

On Thu, Apr 12, 2018 at 4:15 PM, Mike Miller 
wrote:

> While we're on the subject, I've tried to add dicts a few times over the
> years to get a new one but it doesn't work:
>
> d3 = d1 + d2  # TypeError
>
> Thinking a bit, set union is probably a better analogue, but it doesn't
> work either:
>
> d3 = d1 | d2  # TypeError
>
> Where the last value of any duplicate keys should win.
>
> -Mike
>
>
>
> On 2018-04-12 06:46, Andrés Delfino wrote:
>
>> Extending the original idea, IMHO it would make sense for the dict
>> constructor to create a new dictionary not only from several mappings, but
>> mixing mappings and iterables too.
>>
>> Consider this example:
>>
>> x = [(1, 'one')]
>> y = {2: 'two'}
>>
>> Now: {**dict(x), **y}
>> Proposed: dict(x, y)
>>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Mike Miller
While we're on the subject, I've tried to add dicts a few times over the years 
to get a new one but it doesn't work:


d3 = d1 + d2  # TypeError

Thinking a bit, set union is probably a better analogue, but it doesn't work 
either:

d3 = d1 | d2  # TypeError

Where the last value of any duplicate keys should win.

-Mike



On 2018-04-12 06:46, Andrés Delfino wrote:
Extending the original idea, IMHO it would make sense for the dict constructor 
to create a new dictionary not only from several mappings, but mixing mappings 
and iterables too.


Consider this example:

x = [(1, 'one')]
y = {2: 'two'}

Now: {**dict(x), **y}
Proposed: dict(x, y)

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Guido van Rossum
On Thu, Apr 12, 2018 at 7:34 AM, Ed Kellett 
wrote:

> On 2018-04-12 14:46, Andrés Delfino wrote:
> > Extending the original idea, IMHO it would make sense for the dict
> > constructor to create a new dictionary not only from several mappings,
> but
> > mixing mappings and iterables too.
> >
> > Consider this example:
> >
> > x = [(1, 'one')]
> > y = {2: 'two'}
> >
> > Now: {**dict(x), **y}
> > Proposed: dict(x, y)
> >
> > I think this extension makes the call ostensibly easier to read and grep.
>
> It allows for creating a flattened dict from an iterable of dicts, too,
> which I've occasionally wanted:
>
> >>> configs = {'a': 'yes'}, {'b': 'no'}, {'c': 3}
> >>> dict(*configs)
> {'a': 'yes', 'b': 'no', 'c': 3}
>
> versus:
>
> >>> dict(chain.from_iterable(c.items() for c in configs))
> {'a': 'yes', 'b': 'no', 'c': 3}


Yes, this all sounds totally reasonable.

-- 
--Guido van Rossum (python.org/~guido)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Ed Kellett
On 2018-04-12 14:46, Andrés Delfino wrote:
> Extending the original idea, IMHO it would make sense for the dict
> constructor to create a new dictionary not only from several mappings, but
> mixing mappings and iterables too.
> 
> Consider this example:
> 
> x = [(1, 'one')]
> y = {2: 'two'}
> 
> Now: {**dict(x), **y}
> Proposed: dict(x, y)
> 
> I think this extension makes the call ostensibly easier to read and grep.

It allows for creating a flattened dict from an iterable of dicts, too,
which I've occasionally wanted:

>>> configs = {'a': 'yes'}, {'b': 'no'}, {'c': 3}
>>> dict(*configs)
{'a': 'yes', 'b': 'no', 'c': 3}

versus:

>>> dict(chain.from_iterable(c.items() for c in configs))
{'a': 'yes', 'b': 'no', 'c': 3}

Ed



signature.asc
Description: OpenPGP digital signature
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-12 Thread Andrés Delfino
Extending the original idea, IMHO it would make sense for the dict
constructor to create a new dictionary not only from several mappings, but
mixing mappings and iterables too.

Consider this example:

x = [(1, 'one')]
y = {2: 'two'}

Now: {**dict(x), **y}
Proposed: dict(x, y)

I think this extension makes the call ostensibly easier to read and grep. I
believe we are safe regarding compatibility issues, right?

What do you guys think?

On Wed, Apr 11, 2018 at 4:44 AM, Mike Miller 
wrote:

> Ok, we can haggle the finer details and I admit once you learn the syntax
> it isn't substantially harder.  Simply, I've found the dict() a bit easier
> to mentally parse at a glance.  Also, to add I've always expected multiple
> args to work with it, and am always surprised when it doesn't.
>
> Would never have thought of this unpacking syntax if I didn't know that's
> the way its done now, but often have to think about it for a second or two.
>
>
> On 2018-04-10 22:22, Chris Angelico wrote:
>
>> On Wed, Apr 11, 2018 at 2:44 PM, Steven D'Aprano 
>> wrote:
>>
>>> On Wed, Apr 11, 2018 at 02:22:08PM +1000, Chris Angelico wrote:
>>>
>>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-11 Thread Mike Miller
Ok, we can haggle the finer details and I admit once you learn the syntax it 
isn't substantially harder.  Simply, I've found the dict() a bit easier to 
mentally parse at a glance.  Also, to add I've always expected multiple args to 
work with it, and am always surprised when it doesn't.


Would never have thought of this unpacking syntax if I didn't know that's the 
way its done now, but often have to think about it for a second or two.



On 2018-04-10 22:22, Chris Angelico wrote:

On Wed, Apr 11, 2018 at 2:44 PM, Steven D'Aprano  wrote:

On Wed, Apr 11, 2018 at 02:22:08PM +1000, Chris Angelico wrote:


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-10 Thread Chris Angelico
On Wed, Apr 11, 2018 at 2:44 PM, Steven D'Aprano  wrote:
> On Wed, Apr 11, 2018 at 02:22:08PM +1000, Chris Angelico wrote:
>
>> > dict(d1, d2, d3)
>>
>> That's more readable than {**d1, **d2, **d3} ? Doesn't look materially
>> different to me.
>
> It does to me.
>
> On the one hand, we have a function call (okay, technically a type...)
> "dict()" that can be googled on, with three arguments; on the other
> hand, we have syntax that looks like a set {...} and contains the
> obscure ** prefix operator which is hard to google for.

True, you can google 'dict'. But the double-star operator is exactly
the same as is used in kwargs, and actually, I *can* search for it.

https://www.google.com.au/search?q=python+**

Lots of results for kwargs, which is a good start. (DuckDuckGo is less
useful here, though it too is capable of searching for "**". It just
gives more results about exponentiation than about packing/unpacking.)
 The googleability argument may have been a killer a few years ago,
but search engines get smarter every day [1], and it's most definitely
possible to search for operators. Or at least some of them; Google and
DDG don't give me anything useful for "python @".

ChrisA

[1] and a search engine can help you find SmarterEveryDay, not that he
talks about Python
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-10 Thread Steven D'Aprano
On Wed, Apr 11, 2018 at 02:22:08PM +1000, Chris Angelico wrote:

> > dict(d1, d2, d3)
> 
> That's more readable than {**d1, **d2, **d3} ? Doesn't look materially
> different to me.

It does to me.

On the one hand, we have a function call (okay, technically a type...) 
"dict()" that can be googled on, with three arguments; on the other 
hand, we have syntax that looks like a set {...} and contains the 
obscure ** prefix operator which is hard to google for.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-10 Thread Chris Angelico
On Wed, Apr 11, 2018 at 11:19 AM, Mike Miller  wrote:
>
> On 2018-04-09 04:23, Daniel Moisset wrote:
>>
>> In which way would this be different to {**mapping1, **mapping2,
>> **mapping3} ?
>
>
> That's possible now, but believe the form mentioned previously would be more
> readable:
>
> dict(d1, d2, d3)
>

That's more readable than {**d1, **d2, **d3} ? Doesn't look materially
different to me.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-10 Thread Mike Miller


On 2018-04-09 04:23, Daniel Moisset wrote:

In which way would this be different to {**mapping1, **mapping2, **mapping3} ?


That's possible now, but believe the form mentioned previously would be more 
readable:


dict(d1, d2, d3)

-Mike

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-09 Thread Daniel Moisset
No worries, already implemented features happens so often in this list that
there's a story about Guido going back in a time machine to implement them
;-)

Just wanted to check that I had understood what you suggested correctly

On 9 April 2018 at 12:42, Andrés Delfino  wrote:

> Sorry, I didn't know that kwargs unpacking in dictionaries displays don't
> raise a TypeError exception.
>
> On Mon, Apr 9, 2018 at 8:23 AM, Daniel Moisset 
> wrote:
>
>> In which way would this be different to {**mapping1, **mapping2,
>> **mapping3} ?
>>
>> On 8 April 2018 at 22:18, Andrés Delfino  wrote:
>>
>>> Hi!
>>>
>>> I thought that maybe dict could accept several mappings as positional
>>> arguments, like this:
>>>
>>> class Dict4(dict):
 def __init__(self, *args, **kwargs):
 if len(args) > 1:
 if not all([isinstance(arg, dict) for arg in args]):
 raise TypeError('Dict4 expected instances of dict since
 multiple positional arguments were passed')

 temp = args[0].copy()

 for arg in args[1:]:
 temp.update(arg)

 super().__init__(temp, **kwargs)
 else:
 super().__init__(*args, **kwargs)

>>>
>>> AFAIK, this wouldn't create compatibility problems, since you can't pass
>>> two positional arguments now anyways.
>>>
>>> It would be useful to solve the "sum/union dicts" discussion, for
>>> example: requests.get(url, params=dict(params, {'foo': bar})
>>>
>>> Whar are your thoughts?
>>>
>>> ___
>>> Python-ideas mailing list
>>> Python-ideas@python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>>
>>
>>
>> --
>> Daniel F. Moisset - UK Country Manager - Machinalis Limited
>> www.machinalis.co.uk 
>> Skype: @dmoisset T: + 44 7398 827139
>>
>> 1 Fore St, London, EC2Y 9DT
>> 
>>
>> Machinalis Limited is a company registered in England and Wales.
>> Registered number: 10574987.
>>
>
>


-- 
Daniel F. Moisset - UK Country Manager - Machinalis Limited
www.machinalis.co.uk 
Skype: @dmoisset T: + 44 7398 827139

1 Fore St, London, EC2Y 9DT

Machinalis Limited is a company registered in England and Wales. Registered
number: 10574987.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-09 Thread Andrés Delfino
Sorry, I didn't know that kwargs unpacking in dictionaries displays don't
raise a TypeError exception.

On Mon, Apr 9, 2018 at 8:23 AM, Daniel Moisset 
wrote:

> In which way would this be different to {**mapping1, **mapping2,
> **mapping3} ?
>
> On 8 April 2018 at 22:18, Andrés Delfino  wrote:
>
>> Hi!
>>
>> I thought that maybe dict could accept several mappings as positional
>> arguments, like this:
>>
>> class Dict4(dict):
>>> def __init__(self, *args, **kwargs):
>>> if len(args) > 1:
>>> if not all([isinstance(arg, dict) for arg in args]):
>>> raise TypeError('Dict4 expected instances of dict since
>>> multiple positional arguments were passed')
>>>
>>> temp = args[0].copy()
>>>
>>> for arg in args[1:]:
>>> temp.update(arg)
>>>
>>> super().__init__(temp, **kwargs)
>>> else:
>>> super().__init__(*args, **kwargs)
>>>
>>
>> AFAIK, this wouldn't create compatibility problems, since you can't pass
>> two positional arguments now anyways.
>>
>> It would be useful to solve the "sum/union dicts" discussion, for
>> example: requests.get(url, params=dict(params, {'foo': bar})
>>
>> Whar are your thoughts?
>>
>> ___
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
>
> --
> Daniel F. Moisset - UK Country Manager - Machinalis Limited
> www.machinalis.co.uk 
> Skype: @dmoisset T: + 44 7398 827139
>
> 1 Fore St, London, EC2Y 9DT
> 
>
> Machinalis Limited is a company registered in England and Wales.
> Registered number: 10574987.
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-09 Thread Daniel Moisset
In which way would this be different to {**mapping1, **mapping2,
**mapping3} ?

On 8 April 2018 at 22:18, Andrés Delfino  wrote:

> Hi!
>
> I thought that maybe dict could accept several mappings as positional
> arguments, like this:
>
> class Dict4(dict):
>> def __init__(self, *args, **kwargs):
>> if len(args) > 1:
>> if not all([isinstance(arg, dict) for arg in args]):
>> raise TypeError('Dict4 expected instances of dict since
>> multiple positional arguments were passed')
>>
>> temp = args[0].copy()
>>
>> for arg in args[1:]:
>> temp.update(arg)
>>
>> super().__init__(temp, **kwargs)
>> else:
>> super().__init__(*args, **kwargs)
>>
>
> AFAIK, this wouldn't create compatibility problems, since you can't pass
> two positional arguments now anyways.
>
> It would be useful to solve the "sum/union dicts" discussion, for example:
> requests.get(url, params=dict(params, {'foo': bar})
>
> Whar are your thoughts?
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
Daniel F. Moisset - UK Country Manager - Machinalis Limited
www.machinalis.co.uk 
Skype: @dmoisset T: + 44 7398 827139

1 Fore St, London, EC2Y 9DT

Machinalis Limited is a company registered in England and Wales. Registered
number: 10574987.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Accepting multiple mappings as positional arguments to create dicts

2018-04-08 Thread Andrés Delfino
 Hi!

I thought that maybe dict could accept several mappings as positional
arguments, like this:

class Dict4(dict):
> def __init__(self, *args, **kwargs):
> if len(args) > 1:
> if not all([isinstance(arg, dict) for arg in args]):
> raise TypeError('Dict4 expected instances of dict since
> multiple positional arguments were passed')
>
> temp = args[0].copy()
>
> for arg in args[1:]:
> temp.update(arg)
>
> super().__init__(temp, **kwargs)
> else:
> super().__init__(*args, **kwargs)
>

AFAIK, this wouldn't create compatibility problems, since you can't pass
two positional arguments now anyways.

It would be useful to solve the "sum/union dicts" discussion, for example:
requests.get(url, params=dict(params, {'foo': bar})

Whar are your thoughts?
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/