1

Look at this:

>>> eval("assert(True)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    assert(True)
         ^
SyntaxError: invalid syntax

Evaling other functions like this works fine:

>>> eval("str(5)")
'5'

So why does evaling assert fail??

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

6

eval is for expressions. assert is a statement. You seem to think it's a function, but it's not.

You could exec an assert, if you wanted to for some reason.

this = silly = []
exec('assert this is silly')
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • If assert is a statement why does it allow open and close parenthesis after it? – Saqib Ali Apr 09 '18 at 06:25
  • 2
    @SaqibAli `assert` does not even know about those parentheses in the grammar, again because it is a simple statement. `(True) == True` – miradulo Apr 09 '18 at 06:44