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

Soni L. fakedme+py at gmail.com
Tue Nov 28 13:58:42 EST 2017



On 2017-11-28 04:27 PM, Ivan Pozdeev via Python-ideas wrote:
> On 28.11.2017 20:23, Ethan Furman wrote
>
>> On 11/28/2017 08:03 AM, Ivan Pozdeev via Python-ideas wrote:
>>> On 28.11.2017 16:36, Nick Coghlan wrote:
>>
>>>>    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
>>
>> The bulk of any processing in assert (or ensure()) should be the 
>> actual check -- if that fails, only state information should be 
>> included in the exception as anything more complicated runs the risk 
>> of also being wrong as the code is now in a failed state. In other 
>> words, the "error-related arguments" will often be extremely cheap 
>> compared to the test itself.
>>
> My experience is the contrary. The check is usually trivial -- a type 
> check or comparison. While a useful error message contains problem 
> details, so it incorporates string formatting from a variable or two 
> or expressions thereof, like "expected <type>, got <type(var)>".

ensure(check, lambda: raise TypeError("expected <type>, got <type(var)>"))?

>> -- 
>> ~Ethan~
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>



More information about the Python-ideas mailing list