'Why is using not() operator sometimes a SyntaxError? [duplicate]
This works:
>>> not(True)
False
>>> a = {}
>>> a["hidden"] = False
>>> a["hidden"] = not(a["hidden"])
>>> a["hidden"]
True
but not this:
def toggleHelp(self, event):
# https://stackoverflow.com/questions/10267465/showing-and-hiding-widgets#10268076
if (self.special_frame["hidden"] == False):
self.special_frame.grid_remove()
else:
self.special_frame.grid()
self.special_frame["hidden"] == not(self.special_frame["hidden"])
error
line 563
self.special_frame["hidden"] == not(self.special_frame["hidden"])
^
SyntaxError: invalid syntax
in the init:
self.special_frame["hidden"] = False
What I am doing wrong ?
Solution 1:[1]
The problem is the use of ==
where you need =
. This normally wouldn't cause a syntax error, but in your case, you have:
a == not(b)
which is the same as:
a == not b
This groups as:
(a == not) b
and that causes the syntax error.
An assignment operator, on the other hand, has lower precedence, so:
a = not b
groups as:
a = (not b)
which is fine.
Solution 2:[2]
I'm pretty sure you only need one equal sign, maybe that's the mistake.
=
is for assignement and ==
is used for comparison.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Rayane Bouslimi |