mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
adding in-place operators to the operator module.
This commit is contained in:
parent
ecc275bcef
commit
f5bd3b442d
3 changed files with 190 additions and 1 deletions
|
@ -412,6 +412,53 @@ class OperatorTestCase(unittest.TestCase):
|
|||
self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
|
||||
self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)
|
||||
|
||||
def test_inplace(self):
|
||||
class C(object):
|
||||
def __iadd__ (self, other): return "iadd"
|
||||
def __iand__ (self, other): return "iand"
|
||||
def __idiv__ (self, other): return "idiv"
|
||||
def __ifloordiv__(self, other): return "ifloordiv"
|
||||
def __ilshift__ (self, other): return "ilshift"
|
||||
def __imod__ (self, other): return "imod"
|
||||
def __imul__ (self, other): return "imul"
|
||||
def __ior__ (self, other): return "ior"
|
||||
def __ipow__ (self, other): return "ipow"
|
||||
def __irshift__ (self, other): return "irshift"
|
||||
def __isub__ (self, other): return "isub"
|
||||
def __itruediv__ (self, other): return "itruediv"
|
||||
def __ixor__ (self, other): return "ixor"
|
||||
def __getitem__(self, other): return 5 # so that C is a sequence
|
||||
c = C()
|
||||
self.assertEqual(operator.iadd (c, 5), "iadd")
|
||||
self.assertEqual(operator.iand (c, 5), "iand")
|
||||
self.assertEqual(operator.idiv (c, 5), "idiv")
|
||||
self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
|
||||
self.assertEqual(operator.ilshift (c, 5), "ilshift")
|
||||
self.assertEqual(operator.imod (c, 5), "imod")
|
||||
self.assertEqual(operator.imul (c, 5), "imul")
|
||||
self.assertEqual(operator.ior (c, 5), "ior")
|
||||
self.assertEqual(operator.ipow (c, 5), "ipow")
|
||||
self.assertEqual(operator.irshift (c, 5), "irshift")
|
||||
self.assertEqual(operator.isub (c, 5), "isub")
|
||||
self.assertEqual(operator.itruediv (c, 5), "itruediv")
|
||||
self.assertEqual(operator.ixor (c, 5), "ixor")
|
||||
self.assertEqual(operator.iconcat (c, c), "iadd")
|
||||
self.assertEqual(operator.irepeat (c, 5), "imul")
|
||||
self.assertEqual(operator.__iadd__ (c, 5), "iadd")
|
||||
self.assertEqual(operator.__iand__ (c, 5), "iand")
|
||||
self.assertEqual(operator.__idiv__ (c, 5), "idiv")
|
||||
self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
|
||||
self.assertEqual(operator.__ilshift__ (c, 5), "ilshift")
|
||||
self.assertEqual(operator.__imod__ (c, 5), "imod")
|
||||
self.assertEqual(operator.__imul__ (c, 5), "imul")
|
||||
self.assertEqual(operator.__ior__ (c, 5), "ior")
|
||||
self.assertEqual(operator.__ipow__ (c, 5), "ipow")
|
||||
self.assertEqual(operator.__irshift__ (c, 5), "irshift")
|
||||
self.assertEqual(operator.__isub__ (c, 5), "isub")
|
||||
self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
|
||||
self.assertEqual(operator.__ixor__ (c, 5), "ixor")
|
||||
self.assertEqual(operator.__iconcat__ (c, c), "iadd")
|
||||
self.assertEqual(operator.__irepeat__ (c, 5), "imul")
|
||||
|
||||
def test_main(verbose=None):
|
||||
import sys
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue