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

前端 未结 4 894
夕颜
夕颜 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条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 06:11

    First, if you are a beginner, don't worry what is more pythonic or not. This smell language wars, and you will eventually find your own opinion anyway. Just use the way you think is more readable/simpler for you and you will find the light, I think.

    That said, I agree with Alex's excellent answer (as he is always right) and I would add an additional comment why you should prefer the second method - the first will only work correctly if val is a string.

提交回复
热议问题