On 12Aug2021 14:05, [email protected] <[email protected]> wrote:
>This is how slices are used in the python standard library, indeed, but that
>does not stop me from interpreting the slices as "inclusive by default" in my
>library.
>The inconsistency with the rest of the python standard library could be
>misleading, but I think maybe less than off-by-1 errors?
I think the misleadingness is identical to off-by-1 errors.
>You raise a good point however, that is: how to write a slice with
>expliciting the inclusiveness of one of the limit values?
Well, I have seen this done using a combination of square and round
brackets:
[12,19)
which, IIRC, means inclusive on the left end (from the "[") and
exclusive on the right (from the ")"). Obviously this would be a
disaster in Python code, alas.
I do like the idea of bare slices but I also almost never make them. The
is perhaps because they're tedious to type. Instead I find myself doing
one of 2 things:
- a function f(low,high) returning something to do with that range
- a instance method __getitem__ handling slices, so the user can write
x[12:15] directly. Not creating a slice directly, but getting that
range from "x", a sliceable object
Have you looked at SQLAlchemy's core SQL stuff? Tables have columns, so
you can write:
the_table.c.column_name
to reference the column "column_name". That is actually a reference to a
column. It lets you write:
the_table.c.column_name >= 12 and the_table.c.column_name < 15
in code and get SQL out the end. Of course the concise way is sometimes
col = the_table.c.column_name
col >= 12 and col < 15
You can probably even write:
12 <= col < 15
using the normal Python syntax. This works because a column reference
implemenets __lt__ and friends, so that Python comparisons return "SQL
comparison objects".
You could implement such objects too. And have then support slicing via
__getitem__. That might return some kind of "range of column values"
objects, which could be used in expressions like this:
attr[12:15]
Unfortunately that looks to my eye like "get me these elements" rather
than a test, but in the right context (your queries) it might be
intuitive.
Cheers,
Cameron Simpson <[email protected]>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/UTYYHEQPATCWDR3FZX7HMQSSGN4LAJ5Q/
Code of Conduct: http://python.org/psf/codeofconduct/