Why doesn't Python have a sign function?

前端 未结 12 708
旧巷少年郎
旧巷少年郎 2020-12-02 04:54

I can\'t understand why Python doesn\'t have a sign function. It has an abs builtin (which I consider sign\'s sister), but no si

12条回答
  •  庸人自扰
    2020-12-02 05:21

    It just doesn't.

    The best way to fix this is:

    sign = lambda x: bool(x > 0) - bool(x < 0)
    

提交回复
热议问题