On Mon, Oct 07, 2019 at 02:22:22PM +0100, Rhodri James wrote:
> On 04/10/2019 20:34, Caleb Donovick wrote:

> >```
> >where_x_1 = db[x=1]
> >```

which would be equivalent to the existing syntax

   where_x_1 = db[{'x': 1}]


[Rhodi]
> OK, I'm not sure what you're trying to do here, which all on its own 
> says that what you're doing isn't self-explanatory.

I think it is pretty self-explanatory. Caleb wants to allow passing
arguments to ``__getitem__`` by keyword, not just position.

Subscripting obj[x] is, effectively, an alternate form of function call 
syntax with a number of differences and restrictions compared to obj(x):

- calls the ``__getitem__`` dunder instead of ``__call__`` dunder

- by convention, is supposed to be used for item indexing, key 
  lookups, and type annotations, rather than arbitrary uses

- the syntax is different, and highly restricted, compared to regular 
  function calls:

  1. there is no zero-argument form
  2. the one argument form can accept any object (except a slice)
  3. but only by position ``obj[x]`` not by keyword ``obj[spam=x]``
  4. the two and three argument forms use colons as seperators, 
     rather than commas: ``obj[a:b:c]``

Caleb wants to remove the restriction 3.


> Would I be right in 
> thinking you want a shorthand for:
> 
> where_x_1 = [k for k,v in db if v == 1]
>
> If so, I'd rather the comprehension, thanks.  It at least says what it does.

I know what the list comp does because if I stare at it for a minute or 
two, and mentally run through the code, I can work out that it's 
equivalent to iterating over the db (which gives key,value pairs), and 
returning the key if the value equals one.

I'd rather hide the implementation details behind a descriptive method 
name, or a documented query syntax. Just as we prefer to say:

    chunk = sequence[1:-2]

rather than

    chunk = [sequence[i] for i in range(n:=len(sequence)) if 1 <= i < n - 2]

I'm sure the list comp is more explicit and "says what it does" but I'm 
even more sure that you could count the number of experienced Python 
developers who prefer that over a slice on the fingers of one hand.



-- 
Steven
_______________________________________________
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/K7HEZJLCCVLSGHS2U244I7JEHZ5K7DJN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to