3 ms·
> the way this script calls raise() That actually isn't a function call, despite the appearance. Python 3 raise is still a statement, not a function (unlike pr
by pdonis 7y ago
> the way this script calls raise()
That actually isn't a function call, despite the appearance. Python 3 raise is still a statement, not a function (unlike print). So this
raise(ValueError, "some text")
does not do what you think; the proper way in Python 3 to spell what you probably expect is
raise ValueError("some text")
Here is a transcript of a shell session showing the difference:
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> raise ValueError("some text")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: some text
>>> raise(ValueError, "some text")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must derive from BaseException
Note that the exception you actually get the second time isn't even ValueError, it's TypeError; it's being raised because "some text" is a string, not an instance of an exception class derived from BaseException.
In other words, you should not be treating raise in Python 3 the way print is treated. The 2to3 tool treats it properly.