为什么减号的使用不明确为*(-1)? [关闭]

问题描述 投票:0回答:0

引起我注意的是,在 Python 中,您只能在负值前面加上“-”字符,而不是显式乘以 (-1)。 我的意思是这具有世界的所有意义,但是为什么在 num 类型前面加上“-”被解释为乘以(-1)是有原因的? 这只是一个约定? 相关:我认为(不确定)在 Windows 的本机计算器中,当您以负值“-a”开始表达式时,它会显示明确的“0-a”

我试过:

>>> def b():
...     return -b
... 
>>> b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in b
TypeError: bad operand type for unary -: 'function'

或与

>>> def h():
...     return -'h'
... 
>>> h()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in h
TypeError: bad operand type for unary -: 'str'
python conventions negative-number
© www.soinside.com 2019 - 2024. All rights reserved.