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
c779515a
Kaydet (Commit)
c779515a
authored
Tem 12, 2010
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix definition of len() and indexing for memoryview objects (part of #7696).
üst
2efaf967
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
9 deletions
+29
-9
stdtypes.rst
Doc/library/stdtypes.rst
+29
-9
No files found.
Doc/library/stdtypes.rst
Dosyayı görüntüle @
c779515a
...
@@ -2264,10 +2264,19 @@ is generally interpreted as simple bytes.
...
@@ -2264,10 +2264,19 @@ is generally interpreted as simple bytes.
buffer protocol. Builtin objects that support the buffer protocol include
buffer protocol. Builtin objects that support the buffer protocol include
:class:`bytes` and :class:`bytearray`.
:class:`bytes` and :class:`bytearray`.
``len(view)`` returns the total number of bytes in the memoryview, *view*.
A :class:`memoryview` has the notion of an *element*, which is the
atomic memory unit handled by the originating object *obj*. For many
simple types such as :class:`bytes` and :class:`bytearray`, an element
is a single byte, but other types such as :class:`array.array` may have
bigger elements.
``len(view)`` returns the total number of elements in the memoryview,
*view*. The :class:`~memoryview.itemsize` attribute will give you the
number of bytes in a single element.
A :class:`memoryview` supports slicing to expose its data. Taking a single
A :class:`memoryview` supports slicing to expose its data. Taking a single
index will return a single byte. Full slicing will result in a subview::
index will return a single element as a :class:`bytes` object. Full
slicing will result in a subview::
>>> v = memoryview(b'abcefg')
>>> v = memoryview(b'abcefg')
>>> v[1]
>>> v[1]
...
@@ -2278,11 +2287,8 @@ is generally interpreted as simple bytes.
...
@@ -2278,11 +2287,8 @@ is generally interpreted as simple bytes.
<memory at 0x77ab28>
<memory at 0x77ab28>
>>> bytes(v[1:4])
>>> bytes(v[1:4])
b'bce'
b'bce'
>>> v[3:-1]
<memory at 0x744f18>
>>> bytes(v[4:-1])
If the object the memory
view is over supports changing its data, the
If the object the memoryview is over supports changing its data, the
memoryview supports slice assignment::
memoryview supports slice assignment::
>>> data = bytearray(b'abcefg')
>>> data = bytearray(b'abcefg')
...
@@ -2302,12 +2308,18 @@ is generally interpreted as simple bytes.
...
@@ -2302,12 +2308,18 @@ is generally interpreted as simple bytes.
Notice how the size of the memoryview object cannot be changed.
Notice how the size of the memoryview object cannot be changed.
:class:`memoryview` has two methods:
:class:`memoryview` has two methods:
.. method:: tobytes()
.. method:: tobytes()
Return the data in the buffer as a bytestring.
Return the data in the buffer as a bytestring. This is equivalent to
calling the :class:`bytes` constructor on the memoryview. ::
>>> m = memoryview(b"abc")
>>> m.tobytes()
b'abc'
>>> bytes(m)
b'abc'
.. method:: tolist()
.. method:: tolist()
...
@@ -2325,7 +2337,15 @@ is generally interpreted as simple bytes.
...
@@ -2325,7 +2337,15 @@ is generally interpreted as simple bytes.
.. attribute:: itemsize
.. attribute:: itemsize
The size in bytes of each element of the memoryview.
The size in bytes of each element of the memoryview::
>>> m = memoryview(array.array('H', [1,2,3]))
>>> m.itemsize
2
>>> m[0]
b'\x01\x00'
>>> len(m[0]) == m.itemsize
True
.. attribute:: shape
.. attribute:: shape
...
...
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