What does a plus sign do in front of a variable in Python?

后端 未结 2 1435
醉话见心
醉话见心 2021-01-17 10:02

There\'s the following bit of Python code in a project I have to maintain:

# If the `factor` decimal is given, compute new price and a delta
factor = +factor         


        
2条回答
  •  我在风中等你
    2021-01-17 10:09

    I ran into this same problem when I wrongly assumed that Python must support the C increment (++) operator; it doesn't! Instead, it applies the plus-sign operator (+) twice! Which does nothing twice, I soon learned. However, because "++n" looked valid... not flagged as a syntax error... I created a terrible bug for myself.

    So unless you redefine what it does, unary + actually does nothing. Unary - changes from positive to negative and vice-versa, which is why "--n" is also not flagged as a syntax error but it also does nothing.

提交回复
热议问题