<p dir="ltr">On Jul 1, 2014 8:06 PM, "Chris Angelico" <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> wrote:<br>
><br>
> [...]<br>
><br>
> For the OP's use-case, though, it would actually be possible to abuse<br>
> slice notation. I don't remember this being mentioned, but it does<br>
> preserve order; the cost is that all the "keywords" have to be defined<br>
> as objects.<br>
><br>
> class kw: pass # because object() doesn't have attributes<br>
> def make_kw(names):<br>
>     for i in names.split():<br>
>         globals()[i] = obj = kw()<br>
>         obj.keyword_arg = i<br>
> make_kw("Z R X")<br>
><br>
> # Now you can use them in indexing<br>
> some_obj[5, Z:3]<br>
> some_obj[7, Z:3, R:4]<br>
><br>
> The parameters will arrive in the item tuple as slice objects, where<br>
> the start is a signature object and the stop is its value.<br>
><br>
> >>> some_obj[5, Z:3]<br>
> getitem: (5, slice(<__main__.kw object at 0x016C5E10>, 3, None))<br>
><br>
> Yes, it uses a colon rather than an equals sign, but on the flip side,<br>
> it already works :)</p>
<p dir="ltr">This works great, IIRC you can pretty much pass *anything*:</p>
<p dir="ltr">dict[{}:]<br>
dict[AType:lambda x: x]<br>
dict[::]<br>
dict[:]</p>
<p dir="ltr">...don't forget extended slice possibilities :)</p>
<p dir="ltr">I've dabbled with this in custom dict implementations and it usefully excludes all normal dicts, which quickly reject slice objects.</p>
<p dir="ltr">-- </p>
<p dir="ltr">C Anthony [mobile]</p>