On Tuesday, June 5, 2018 at 12:19:53 AM UTC-7, Ben Rudiak-Gould wrote:
>
> When the loop is simple enough I can write
>
> items = [... for item in items]
>
> and when it's complicated enough it probably makes sense to split it
> into a separate function. But I've many times wished that I
On Mon, Jun 4, 2018 at 5:11 PM, Steven D'Aprano wrote:
> class MyList(list):
> def pop(self, pos):
> if isinstance(pos, slice):
> temp = self[pos]
> del self[pos]
> return temp
> return super().pop(pos)
>
> Is that what you have in mind?
On Mon, Jun 04, 2018 at 03:23:07PM -0700, Ben Rudiak-Gould wrote:
> The `pop` method of built-in sequences is basically an atomic version of
>
> val = self[pos]
> del self[pos]
> return val
Aside from the atomicness, for testing we can subclass list:
# Untested
class MyList(list):
The `pop` method of built-in sequences is basically an atomic version of
val = self[pos]
del self[pos]
return val
If this behavior was extended to the case where `pos` is a slice, you
could write things like:
def cut_deck(deck, pos):
deck.extend(deck.pop(slice(0, pos)))