1

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 ?

martineau
  • 119,623
  • 25
  • 170
  • 301
Walle Cyril
  • 3,087
  • 4
  • 23
  • 55
  • 3
    `not` is used as an operator, not as a function. Bad: `not(True)`; good: `not True`. – Klaus D. Oct 19 '17 at 14:40
  • 1
    The problem is the `==` operator. Change it to `=` and the syntax error will go away. Also, you normally don't want to use `not(x)`, just use `not x` unless you really need the parentheses for some reason. – Tom Karzes Oct 19 '17 at 14:41
  • 1
    The syntax error being caused by a typo (the two examples are *not* the same). – martineau May 02 '22 at 08:12

2 Answers2

3

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.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • 1
    Yup... one could use `a == (not(b))` if one really wanted... but... Anyway - might be useful to link to [operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence)... – Jon Clements Oct 19 '17 at 14:45
  • @JonClements Yes, exactly. – Tom Karzes Oct 19 '17 at 14:46
  • The need to use `not` with parentheses to override precedence is explained in Note 3 of [Boolean Operations — `and`, `or`, `not`](https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not) section of the current documentation. – martineau May 02 '22 at 08:06
0

I'm pretty sure you only need one equal sign, maybe that's the mistake. = is for assignement and == is used for comparison.

Rayane Bouslimi
  • 193
  • 1
  • 1
  • 9