“”.join(reversed(val)) vs val[::-1]…which is pythonic?

前端 未结 4 881
夕颜
夕颜 2020-12-30 05:42

So according to the Zen of Python ... Explicit is better than implicit...Sparse is better than dense...Readability counts...but then agai

4条回答
  •  -上瘾入骨i
    2020-12-30 06:21

    With a string, as you have, I would go with the first option, since it makes it clear that the result you want is a string. For a list or other iterable/slicable, I'd go with the second.

    An added benefit of the first form is it will work if val is not actually a string, but some other iterable of characters (e.g. a generator of some kind). In some cases, this makes a big difference.

提交回复
热议问题