[Python-ideas] Repurpose `assert' into a general-purpose check

Nick Coghlan ncoghlan at gmail.com
Tue Nov 28 23:39:53 EST 2017


On 29 November 2017 at 02:03, Ivan Pozdeev via Python-ideas
<python-ideas at python.org> wrote:
> On 28.11.2017 16:36, Nick Coghlan wrote:
>> I'll make the same observation I usually do each time one of these
>> threads comes up:
>>
>> * I'm opposed to making assert substantially different from the way it
>> works now
>> * I'm in favour of adding a new "ensure()" builtin that encapsulates
>> the check-and-raise logic
>>
>> The reasons I prefer this approach:
>>
>> - assert is a statement *solely* so that the compiler can optimise it
>> out. If it's not optional,
>>    it doesn't need to be a statement any more
>
> Another benefit of a statement vs function is only evaluating the
> error-related arguments when there's an error

If you're worried about that level of micro-optimisation, it's
straightforward enough to write your own wrapper function that accepts
the components needed to build a suitably formatted message. There's
no need to make the builtin more complicated to cope with it (that
smooth transition from the builtin behaviour to customised behaviour
is one of the other advantages of using a plain function).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list