Executive summary:

Dicts are unordered, so we can distinguish dict from set by the first
item (no new notation), and after that default identifiers to (name :
in-scope value) items.  Also some notational bikeshedding.

Atsuo Ishimoto writes:

 > It is sometimes tedious to write a dictionary in Python. For example,

 >     def register_user(first, last, addr1, addr2):
 >         d = {'first': first,
 >              'last': last,
 >              'addr1': addr1,
 >              'addr2': addr2,
 >              'tel': '123-456-789'}
 > 
 >         requests.post(URL, d)

In this particular case,

    def register_user(first, last, addr1, addr2):
        d = locals().copy()    # .copy is unnecessary in this case,
                               # but note that the next line may
                               # pollute the locals
        d['tel'] = '123-456-789'

        requests.post(URL, d)

DTRTs.  How often would locals() be usable in this way?  Note: in the
case of requests, this might be a vulnerability, because the explicit
dict display would presumably include only relevant items, while
locals() might inherit private credentials from the arguments, which
need to be explicitly del'ed from d.

 > The dict literal contains a lot of duplicated words and quotation
 > marks. Using dict type looks nicer, but still verbose.
 > 
 >     d = dict(first=first,
 >              last=last,
 >              addr1=addr1,
 >              addr2=addr2,
 >              tel='123-456-789')
 > 
 > With recent JavaScript, the same object can be written more easily.
 > 
 >     d = {first, last, addr1, addr2, tel='123-456-789'}
 > 
 > How about adding similar syntax to Python? Like raw strings, we can
 > add prefix letters such as '$' to the opening curly brace for the
 > purpose.

I understand that this was done for ease of your POC implementatation,
and you prefer a letter.  But I'd like to emphasize:  Please don't use
$ for this.  Among other things, it is both in appearance and
historically based on "S" for "set"!

Also, please use dict display syntax (':' not '=').

If you're going to use prefix characters, I suggest 'd' for "dict",
and maybe 's' for "set" as well (to allow the use case 's{}' for the
empty set, though that's not terribly useful vs. set(). I'm mostly
proposing it so I be the first to say "-1" on 's{}'. :-)

This proposal does make me a more sympathetic to such abbreviations.
I'm still at best +0 on it, though.

It occurs to me there's an alternative syntax with even less notation:

    d = {'tel' : '123-456-789', first, last, addr1, addr2}

I.e, if the first member of the display is a dict item, it's a dict,
and the rest of the members default to key = name of identifier and
value = value of identifier.  If 'tel' weren't a key in d you'd write

    d = {'first' : first, last, addr1, addr2}

A little awkward, but in many cases you'll be adding information as
you did.

 > I wrote a simple POC implementation here. It looks working.

Thank you!
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UZZUEWGV3WUHQRUR5WDWYTCAALP6OD6V/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to