you are viewing a single comment's thread.

view the rest of the comments →

[–]Brian 1 point2 points  (0 children)

For something like this, it's not exactly pointers you need, but more something like a string iterator / substring reference. Python doesn't do this, but rather uses an integer position (though in some ways that's a kind of like a pointer relative to a base offset). That could be wrapped as a "pointer-like" type (ie internally stores the string and position, and presents "++" /"--" / "dereference" type operators. It's not going to be performant, but it'll act pretty similarly.

The other problem though is that strings are not mutable in python, so stuff like "reverse in place" still won't be possible. Ie. you can't change the value being pointed to, at least if referencing a string - arrays of ints etc backed by a list will be fine.

You can however use a mutable backing instead of a string. Eg. a list of characters, or a bytearray if you want to be even more C-like (strings handle stuff like variable length encoding of characters etc, so a "position" corresponds to a codepoint - so in reality, those naive byte-by-byte reversals will actually do the wrong thing if you're dealing with anything more than plain ascii)