I agree, assert should be a built-in function, rather than a keyword. It was overlooked when print() tore the world apart with 3.0, so I think it's safe to say that it have had very little impact.
I'm all for changing it. It will just have to go through __future__ purgatory for a decade or so, before I'm happy telling people to no longer rely on asserting that their tuple is non-empty.
Well, there is, in that that'd still incur pretty much all the same overhead as when present.
You'd still pay the overhead of the function call, and of evaluating the arguments, so something like
assert(some_expensive_check(), "Something went wrong")
Will still pay the whole price of the check. The only thing you save is the actual "is this condition true" check, which is pretty inconsequential. Really, you might as well not bother changing behaviour.
If you want to make it equivalent to current assert, you'd need to do more: not just make the function a no-op, but also suppress evaluation of the arguments being passed to it, which you can't do as just a normal function. I think that'd be worse: making it look more like a function, but actually having it special cased to perform very differently.
Well, there is, in that that'd still incur pretty much all the same overhead as when present.
You'd still pay the overhead of the function call, and of evaluating the arguments, so something like
You forget that we're discussing relative to a PEP that will require changes to the parser. As you seem comitted to do so, I don't really see a fundamental difference in leaving a statement or a call to a function with a particular name out of the AST, if one of the -O flags are present.
But again, my own preference is not to special case it. People who ignore the warning are definitely asking for the pain.
You forget that we're discussing relative to a PEP that will require changes to the parser
No, but as I mentioned, for this you'd require more than making it a function - you'd also need to suppress evaluation of arguments which removing from the generated code would do, but I do think is worse than leaving it a statement: you're making something that looks like a function, but actually has special-cased behaviour. Even worse, I'm not sure you can really implement it that way only for actual assert: if you're going to make it just another function, then you'd be able to override that name. Do you then special case any function call named assert? Or make it half-way to a keyword and forbid rebinding it.
There are also potential complexities that go beyond just looking though the AST for a function call with the right name. Eg. what will happen with the below:
checker = assert
checker(cond)
[f(cond) for f in [assert, print]]
def assert(*args): pass
assert(cond)
import builtins
builtins.assert(cond)
This isn't just a matter of checking for the AST for function calls with a name being assert - there more complicated issues. Do we unbind the assert function as well, and let these throw NameErrors on optimised builds? Do we go by name only (which seems the only reliable way - it's impossible to be sure what a function is in many cases), meaning checker won't behave like assert, even though it is the assert function, while assert will act like it even though it isn't?
I think if you're going to have assert act differently from other functions - especially if "assert" here means "any function named assert", then I think it'd be better to leave it as a statement, rather than a function whose name triggers magic behaviour in the parser.
21
u/Anonymous_user_2022 Jan 21 '22
I agree, assert should be a built-in function, rather than a keyword. It was overlooked when print() tore the world apart with 3.0, so I think it's safe to say that it have had very little impact.
I'm all for changing it. It will just have to go through __future__ purgatory for a decade or so, before I'm happy telling people to no longer rely on asserting that their tuple is non-empty.