operator module functions

Ned Batchelder ned at nedbatchelder.com
Thu Oct 9 03:41:12 CEST 2014


On 10/8/14 5:49 PM, Ethan Furman wrote:
> [redirecting back to the list]
>
> On 10/08/2014 02:23 PM, random832 at fastmail.us wrote:
>> On Wed, Oct 8, 2014, at 15:53, Ethan Furman wrote:
>>> On 10/08/2014 12:49 PM, random832 at fastmail.us wrote:
>>>> On Wed, Oct 8, 2014, at 15:38, Ethan Furman wrote:
>>>>>
>>>>> LOL, no kidding!  The main reason I bother using the operator
>>>>> module is
>>>>> for the readability of not seeing the dunders,
>>>>> and the writability of not having to type them.
>>>>
>>>> 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)

Don't forget, in 2.7 you can do:

     with self.assertRaises(TypeError):
         obj1 + obj2

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list