diff options
author | Raymond Hettinger <python@rcn.com> | 2015-08-17 02:43:34 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-08-17 02:43:34 (GMT) |
commit | 47d159f5220918f9c1216ba93583f3aa740a8944 (patch) | |
tree | 1e324fa8f68399c96643d09e0168b45f9be2a654 /Lib/operator.py | |
parent | 755cb0ae8d02daa7f231da90c303ebf9a0f3b01f (diff) | |
download | cpython-47d159f5220918f9c1216ba93583f3aa740a8944.zip cpython-47d159f5220918f9c1216ba93583f3aa740a8944.tar.gz cpython-47d159f5220918f9c1216ba93583f3aa740a8944.tar.bz2 |
Issue #24379: Add operator.subscript() as a convenience for building slices.
Diffstat (limited to 'Lib/operator.py')
-rw-r--r-- | Lib/operator.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/Lib/operator.py b/Lib/operator.py index 0e2e53e..bc2a947 100644 --- a/Lib/operator.py +++ b/Lib/operator.py @@ -17,7 +17,7 @@ __all__ = ['abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', - 'setitem', 'sub', 'truediv', 'truth', 'xor'] + 'setitem', 'sub', 'subscript', 'truediv', 'truth', 'xor'] from builtins import abs as _abs @@ -408,6 +408,32 @@ def ixor(a, b): return a +@object.__new__ # create a singleton instance +class subscript: + """ + A helper to turn subscript notation into indexing objects. This can be + used to create item access patterns ahead of time to pass them into + various subscriptable objects. + + For example: + subscript[5] == 5 + subscript[3:7:2] == slice(3, 7, 2) + subscript[5, 8] == (5, 8) + """ + __slots__ = () + + def __new__(cls): + raise TypeError("cannot create '{}' instances".format(cls.__name__)) + + @staticmethod + def __getitem__(key): + return key + + @staticmethod + def __reduce__(): + return 'subscript' + + try: from _operator import * except ImportError: |