On 04/10/2019 20:34, Caleb Donovick wrote:
While there is no restriction on passing dicts to getitem. Doing so tends
to be a bit ugly. I have two main use cases in mind for this syntax.
The first and perhaps the most obvious, is doing relational queries.
```
where_x_1 = db[x=1]
```
is more beautiful than
```
where_x_1 = db[dict(x=1)]
where_x_1 = db[{'x': 1}]
# or by abusing slices
where_x_1 = db['x':1]
# or in the style of Pandas
where_x_1 = db[db['x'] == 1]
```
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. 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.
Beyond relational queries my own personal use case is a shorthand for
dataclasses / protocols.
```
foo: ProtoRecord[x=int, y=int] = DataRecord[x=int, y=int](0, 1)
```
where `DataRecord[field0=T0, ..., fieldk=Tk]` generates
```
@dataclass
class Record:
field0: T0
...
fieldk: Tk
```
and `ProtoRecord[field0=T0, ..., fieldk=Tk]` generates a similar protocol.
Explicit is better than implicit.
Allowing key value pairs in geitem need not change the interface of
getitem. All the key value pairs could be collected as a dict and passed
to getitem as the index.
Um. Stop me if I'm wrong, but isn't that exactly a change to the
interface of getitem?
Sorry, I don't like it.
--
Rhodri James *-* Kynesim Ltd
_______________________________________________
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/ABL3V6KGEZPTPJ2VKL7Y5GAUWFIEZOAI/
Code of Conduct: http://python.org/psf/codeofconduct/