Skip to main content
Jamalize: Fix typos, update grammar, etc; add reference ink
Source Link

I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite waydirection to the one intended.

WhyWhat I might prefer is a reversible view? Something like:

r = l.reverse

In a few circumstances this makes it less likely that you leave the list in the wrong direction, because you are working on a reversed copy.

This stuff is basically doing index arithmetic... which is fine because that's an easy thing to get wrong, and I would prefer this wrapper to forever be writing len(x) - 1. Though I'd note that in practice one avoids usingindexesusing indexes in python whenever possible, and for these purposes for x in reversed(l) does what you want.

One thing to note is that this implementation doesn't support append or pop., Which I use a bit. So you might end up wanting to use a deque under the hood. I'd add that when I use deque'sdeques a common mistake for me is to call pop instead of popleft etc, so having a reversible view on the deque might simplify things a little - I still probably wouldn't use it - before my coworkers would moan at me, but there's no reason you should care about that. I might end up doing somthing like this if I was reversereversing lists a lot.

This is a nice example for understanding and implementing python's dunder methodsdunder methods. I'd throw in that theirthere are abstract base classes: https://docs.python.org/3/library/collections.abc.html#module-collections.abc and a set of interfaces for "types of collections" so you might want to subclass one of these if you want to avoid the possibility of surprising users.

I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite way to the one intended.

Why I might prefer is a reversible view? Something like:

r = l.reverse

In a few circumstances this makes it less likely that you leave the list in the wrong direction, because you are working on a reversed copy.

This stuff is basically doing index arithmetic... which is fine because that's an easy thing to get wrong, and I would prefer this wrapper to forever be writing len(x) - 1. Though I'd note that in practice one avoids usingindexes in python whenever possible, and for these purposes for x in reversed(l) does what you want.

One thing to note is that this implementation doesn't support append or pop. Which I use a bit. So you might end up wanting to use a deque under the hood. I'd add that when I use deque's a common mistake for me is to call pop instead of popleft etc, so having a reversible view on the deque might simplify things a little - I still probably wouldn't use it - before my coworkers would moan at me, but there's no reason you should care about that. I might end up doing somthing like this if I was reverse lists a lot.

This is a nice example for understanding and implementing python's dunder methods. I'd throw in that their are abstract base classes: https://docs.python.org/3/library/collections.abc.html#module-collections.abc and a set of interfaces for "types of collections" so you might want to subclass one of these if you want to avoid the possibility of surprising users.

I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite direction to the one intended.

What I might prefer is a reversible view? Something like:

r = l.reverse

In a few circumstances this makes it less likely that you leave the list in the wrong direction, because you are working on a reversed copy.

This stuff is basically doing index arithmetic... which is fine because that's an easy thing to get wrong, and I would prefer this wrapper to forever be writing len(x) - 1. Though I'd note that in practice one avoids using indexes in python whenever possible, and for these purposes for x in reversed(l) does what you want.

One thing to note is that this implementation doesn't support append or pop, Which I use a bit. So you might end up wanting to use a deque under the hood. I'd add that when I use deques a common mistake for me is to call pop instead of popleft etc, so having a reversible view on the deque might simplify things a little - I still probably wouldn't use it - before my coworkers would moan at me, but there's no reason you should care about that. I might end up doing somthing like this if I was reversing lists a lot.

This is a nice example for understanding and implementing python's dunder methods. I'd throw in that there are abstract base classes: https://docs.python.org/3/library/collections.abc.html#module-collections.abc and a set of interfaces for "types of collections" so you might want to subclass one of these if you want to avoid the possibility of surprising users.

Source Link
Att Righ
  • 261
  • 1
  • 3

I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite way to the one intended.

Why I might prefer is a reversible view? Something like:

r = l.reverse

In a few circumstances this makes it less likely that you leave the list in the wrong direction, because you are working on a reversed copy.

This stuff is basically doing index arithmetic... which is fine because that's an easy thing to get wrong, and I would prefer this wrapper to forever be writing len(x) - 1. Though I'd note that in practice one avoids usingindexes in python whenever possible, and for these purposes for x in reversed(l) does what you want.

One thing to note is that this implementation doesn't support append or pop. Which I use a bit. So you might end up wanting to use a deque under the hood. I'd add that when I use deque's a common mistake for me is to call pop instead of popleft etc, so having a reversible view on the deque might simplify things a little - I still probably wouldn't use it - before my coworkers would moan at me, but there's no reason you should care about that. I might end up doing somthing like this if I was reverse lists a lot.

This is a nice example for understanding and implementing python's dunder methods. I'd throw in that their are abstract base classes: https://docs.python.org/3/library/collections.abc.html#module-collections.abc and a set of interfaces for "types of collections" so you might want to subclass one of these if you want to avoid the possibility of surprising users.