Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13)

Delta Between Two Patch Sets: Doc/library/stdtypes.rst

Issue 15573: Support unknown formats in memoryview comparisons
Left Patch Set: Created 7 years, 7 months ago
Right Patch Set: Created 7 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Doc/whatsnew/3.3.rst » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 .. XXX: reference/datamodel and this have quite a few overlaps! 1 .. XXX: reference/datamodel and this have quite a few overlaps!
2 2
3 3
4 .. _bltin-types: 4 .. _bltin-types:
5 5
6 ************** 6 **************
7 Built-in Types 7 Built-in Types
8 ************** 8 **************
9 9
10 The following sections describe the standard types that are built into the 10 The following sections describe the standard types that are built into the
(...skipping 2482 matching lines...) Expand 10 before | Expand all | Expand 10 after
2493 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]] 2493 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
2494 >>> hash(x) == hash(y) == hash(y.tobytes()) 2494 >>> hash(x) == hash(y) == hash(y.tobytes())
2495 True 2495 True
2496 2496
2497 .. versionchanged:: 3.3 2497 .. versionchanged:: 3.3
2498 Memoryview objects are now hashable. 2498 Memoryview objects are now hashable.
2499 2499
2500 2500
2501 :class:`memoryview` has several methods: 2501 :class:`memoryview` has several methods:
2502 2502
2503 .. method:: __eq__() 2503 .. method:: __eq__(exporter)
2504 2504
2505 Abstract definition of equality: Two memoryviews v and w compare equal 2505 A memoryview and a :pep:`3118` exporter are equal if their shapes are
2506 if v.tolist() == w.tolist(). Contrary to the current implementation of 2506 equivalent and if all corresponding values are equal when the operands'
2507 :meth:`tolist`, all format strings in :mod:`struct` syntax are handled 2507 respective format codes are interpreted using :mod:`struct` syntax.
2508 in comparisons. 2508
loewis 2012/08/14 16:50:53 Proposal for rephrasing the last sentence: "While
skrah 2012/08/14 18:30:59 Yes, I don't like the original either. Also, the d
Nick Coghlan 2012/08/15 06:13:09 Proposed wording that tries to be both accurate an
skrah 2012/08/15 16:45:02 Sounds good to me. In the new patch I changed "two
2509 2509 For the subset of :mod:`struct` format strings currently supported by
2510 If a format string is not in :mod:`struct` syntax or if unpacking a 2510 :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``: :
2511 value results in a struct error, the result of the comparison is 2511
2512 "not equal". Example: :: 2512 >>> import array
2513 >>> a = array.array('I', [1, 2, 3, 4, 5])
2514 >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
2515 >>> c = array.array('b', [5, 3, 1])
2516 >>> x = memoryview(a)
2517 >>> y = memoryview(b)
2518 >>> x == a == y == b
2519 True
2520 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
2521 True
2522 >>> z = y[::-2]
2523 >>> z == c
2524 True
2525 >>> z.tolist() == c.tolist()
2526 True
2527
2528 If either format string is not supported by the :mod:`struct` module,
2529 then the objects will always compare as unequal (even if the format
2530 strings and buffer contents are identical)::
2513 2531
2514 >>> from ctypes import BigEndianStructure, c_long 2532 >>> from ctypes import BigEndianStructure, c_long
2515 >>> class BEPoint(BigEndianStructure): 2533 >>> class BEPoint(BigEndianStructure):
2516 ... _fields_ = [("x", c_long), ("y", c_long)] 2534 ... _fields_ = [("x", c_long), ("y", c_long)]
2517 ... 2535 ...
2518 >>> point = BEPoint(100, 200) 2536 >>> point = BEPoint(100, 200)
2519 >>> a = memoryview(point) 2537 >>> a = memoryview(point)
2520 >>> b = memoryview(point) 2538 >>> b = memoryview(point)
2539 >>> a == point
2540 False
2521 >>> a == b 2541 >>> a == b
2522 False 2542 False
2543
2544 Note that, as with floating point numbers, ``v is w`` does *not* imply
2545 ``v == w`` for memoryview objects.
2523 2546
2524 .. versionchanged:: 3.3 2547 .. versionchanged:: 3.3
2525 2548
2526 .. method:: tobytes() 2549 .. method:: tobytes()
2527 2550
2528 Return the data in the buffer as a bytestring. This is equivalent to 2551 Return the data in the buffer as a bytestring. This is equivalent to
2529 calling the :class:`bytes` constructor on the memoryview. :: 2552 calling the :class:`bytes` constructor on the memoryview. ::
2530 2553
2531 >>> m = memoryview(b"abc") 2554 >>> m = memoryview(b"abc")
2532 >>> m.tobytes() 2555 >>> m.tobytes()
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
3138 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0] ``, and 3161 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0] ``, and
3139 similarly for tuples. 3162 similarly for tuples.
3140 3163
3141 .. [3] They must have since the parser can't tell the type of the operands. 3164 .. [3] They must have since the parser can't tell the type of the operands.
3142 3165
3143 .. [4] Cased characters are those with general category property being one of 3166 .. [4] Cased characters are those with general category property being one of
3144 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecas e). 3167 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecas e).
3145 3168
3146 .. [5] To format only a tuple you should therefore provide a singleton tuple who se only 3169 .. [5] To format only a tuple you should therefore provide a singleton tuple who se only
3147 element is the tuple to be formatted. 3170 element is the tuple to be formatted.
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+