operator module functions

Terry Reedy tjreedy at udel.edu
Thu Oct 9 00:46:05 CEST 2014


On 10/8/2014 5:49 PM, Ethan Furman wrote:
> [redirecting back to the list]

>>>> I'm not sure what situation you would have to type them (as opposed to
>>>> simply a + b) that the operator module would help with.
>>>
>>> unittest springs to mind:
>>>
>>>     self.assertRaises(TypeError, op.add, obj1, obj2)
>>
>> Er, my point is, that is not a situation where you would be able to use
>> obj1.__add__ - you'd have to use the operator module *anyway*, and
>> therefore aren't using it just to get the convenience of a non-dunder
>> name.
>
>   self.assertRaises(TypeError, lambda x, y: x+y, obj1, obj2)

That works, but is a bit harder to type (given the import), more 
confusing to read (the extra ',' that does *not* delimit an argument) 
and adds an extra layer to the call stack with each call.  The extra 
layer *could* make a difference in recursion.

-- 
Terry Jan Reedy




More information about the Python-list mailing list