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

Side by Side Diff: Doc/reference/datamodel.rst

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 4 years, 1 month 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:
View unified diff | Download patch
OLDNEW
1 1
2 .. _datamodel: 2 .. _datamodel:
3 3
4 ********** 4 **********
5 Data model 5 Data model
6 ********** 6 **********
7 7
8 8
9 .. _objects: 9 .. _objects:
10 10
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 1056
1057 A class can implement certain operations that are invoked by special syntax 1057 A class can implement certain operations that are invoked by special syntax
1058 (such as arithmetic operations or subscripting and slicing) by defining methods 1058 (such as arithmetic operations or subscripting and slicing) by defining methods
1059 with special names. This is Python's approach to :dfn:`operator overloading`, 1059 with special names. This is Python's approach to :dfn:`operator overloading`,
1060 allowing classes to define their own behavior with respect to language 1060 allowing classes to define their own behavior with respect to language
1061 operators. For instance, if a class defines a method named :meth:`__getitem__`, 1061 operators. For instance, if a class defines a method named :meth:`__getitem__`,
1062 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent 1062 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent
1063 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n 1063 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n
1064 operation raise an exception when no appropriate method is defined (typically 1064 operation raise an exception when no appropriate method is defined (typically
1065 :exc:`AttributeError` or :exc:`TypeError`). 1065 :exc:`AttributeError` or :exc:`TypeError`).
1066
1067 Setting a special method to ``None`` indicates that the corresponding
storchaka 2016/01/05 21:48:04 This works not with all special methods, isn't?
abarnert 2016/01/05 23:28:55 No, it works with all special methods. (And, accor
1068 operation is not available. For example, if a class sets
1069 :meth:`__iter__` to ``None``, the class is not iterable, so calling
1070 :func:`iter` on its instances will raise a :exc:`TypeError` (without
1071 falling back to :meth:`__getitem__`).
1066 1072
1067 When implementing a class that emulates any built-in type, it is important that 1073 When implementing a class that emulates any built-in type, it is important that
1068 the emulation only be implemented to the degree that it makes sense for the 1074 the emulation only be implemented to the degree that it makes sense for the
1069 object being modelled. For example, some sequences may work well with retrieval 1075 object being modelled. For example, some sequences may work well with retrieval
1070 of individual elements, but extracting a slice may not make sense. (One example 1076 of individual elements, but extracting a slice may not make sense. (One example
1071 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document 1077 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document
1072 Object Model.) 1078 Object Model.)
1073 1079
1074 1080
1075 .. _customization: 1081 .. _customization:
(...skipping 1343 matching lines...) Expand 10 before | Expand all | Expand 10 after
2419 2425
2420 .. rubric:: Footnotes 2426 .. rubric:: Footnotes
2421 2427
2422 .. [#] It *is* possible in some cases to change an object's type, under certain 2428 .. [#] It *is* possible in some cases to change an object's type, under certain
2423 controlled conditions. It generally isn't a good idea though, since it can 2429 controlled conditions. It generally isn't a good idea though, since it can
2424 lead to some very strange behaviour if it is handled incorrectly. 2430 lead to some very strange behaviour if it is handled incorrectly.
2425 2431
2426 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod 2432 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod
2427 (such as :meth:`__add__`) fails the operation is not supported, which is why the 2433 (such as :meth:`__add__`) fails the operation is not supported, which is why the
2428 reflected method is not called. 2434 reflected method is not called.
OLDNEW

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