Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
47d159f5
Kaydet (Commit)
47d159f5
authored
Agu 17, 2015
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #24379: Add operator.subscript() as a convenience for building slices.
üst
755cb0ae
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
2 deletions
+81
-2
operator.rst
Doc/library/operator.rst
+15
-0
3.6.rst
Doc/whatsnew/3.6.rst
+6
-1
operator.py
Lib/operator.py
+27
-1
test_operator.py
Lib/test/test_operator.py
+33
-0
No files found.
Doc/library/operator.rst
Dosyayı görüntüle @
47d159f5
...
...
@@ -333,6 +333,21 @@ expect a function argument.
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
.. data:: 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)``
.. versionadded:: 3.6
.. function:: methodcaller(name[, args...])
Return a callable object that calls the method *name* on its operand. If
...
...
Doc/whatsnew/3.6.rst
Dosyayı görüntüle @
47d159f5
...
...
@@ -95,7 +95,12 @@ New Modules
Improved Modules
================
* None yet.
operator
--------
* New object :data:`operator.subscript` makes it easier to create complex
indexers. For example: ``subscript[0:10:2] == slice(0, 10, 2)``
(Contributed by Joe Jevnik in :issue:`24379`.)
Optimizations
...
...
Lib/operator.py
Dosyayı görüntüle @
47d159f5
...
...
@@ -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
:
...
...
Lib/test/test_operator.py
Dosyayı görüntüle @
47d159f5
...
...
@@ -596,5 +596,38 @@ class CCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
module2
=
c_operator
class
SubscriptTestCase
:
def
test_subscript
(
self
):
subscript
=
self
.
module
.
subscript
self
.
assertIsNone
(
subscript
[
None
])
self
.
assertEqual
(
subscript
[
0
],
0
)
self
.
assertEqual
(
subscript
[
0
:
1
:
2
],
slice
(
0
,
1
,
2
))
self
.
assertEqual
(
subscript
[
0
,
...
,
:
2
,
...
],
(
0
,
Ellipsis
,
slice
(
2
),
Ellipsis
),
)
def
test_pickle
(
self
):
from
operator
import
subscript
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
subTest
(
proto
=
proto
):
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
subscript
,
proto
)),
subscript
,
)
def
test_singleton
(
self
):
with
self
.
assertRaises
(
TypeError
):
type
(
self
.
module
.
subscript
)()
def
test_immutable
(
self
):
with
self
.
assertRaises
(
AttributeError
):
self
.
module
.
subscript
.
attr
=
None
class
PySubscriptTestCase
(
SubscriptTestCase
,
PyOperatorTestCase
):
pass
if
__name__
==
"__main__"
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment