Changeset 391 for python/trunk/Doc/reference/datamodel.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/reference/datamodel.rst
r2 r391 67 67 module for information on controlling the collection of cyclic garbage. 68 68 Other implementations act differently and CPython may change. 69 Do not depend on immediate finalization of objects when they become 70 unreachable (ex: always close files). 69 71 70 72 Note that the use of the implementation's tracing or debugging facilities may … … 410 412 These represent a mutable set. They are created by the built-in :func:`set` 411 413 constructor and can be modified afterwards by several methods, such as 412 :meth:` add`.414 :meth:`~set.add`. 413 415 414 416 Frozen sets … … 479 481 480 482 Special attributes: 483 484 .. tabularcolumns:: |l|L|l| 481 485 482 486 +-----------------------+-------------------------------+-----------+ … … 572 576 573 577 .. versionchanged:: 2.6 574 For 3.0forward-compatibility, :attr:`im_func` is also available as578 For Python 3 forward-compatibility, :attr:`im_func` is also available as 575 579 :attr:`__func__`, and :attr:`im_self` as :attr:`__self__`. 576 580 … … 620 624 621 625 When a user-defined method object is created by retrieving a class method object 622 from a class or instance, its :attr:`im_self` attribute is the class itself (the 623 same as the :attr:`im_class` attribute), and its :attr:`im_func` attribute is 624 the function object underlying the class method. 626 from a class or instance, its :attr:`im_self` attribute is the class itself, and 627 its :attr:`im_func` attribute is the function object underlying the class method. 625 628 626 629 When an unbound user-defined method object is called, the underlying function … … 659 662 function`. Such a function, when called, always returns an iterator object 660 663 which can be used to execute the body of the function: calling the iterator's 661 :meth:`next` method will cause the function to execute until it provides a value 664 :meth:`~iterator.next` method will cause the function to execute until 665 it provides a value 662 666 using the :keyword:`yield` statement. When the function executes a 663 667 :keyword:`return` statement or falls off the end, a :exc:`StopIteration` … … 690 694 a built-in method is ``alist.append()``, assuming *alist* is a list object. In 691 695 this case, the special read-only attribute :attr:`__self__` is set to the object 692 denoted by * list*.696 denoted by *alist*. 693 697 694 698 Class Types … … 739 743 Special read-only attribute: :attr:`__dict__` is the module's namespace as a 740 744 dictionary object. 745 746 .. impl-detail:: 747 748 Because of the way CPython clears module dictionaries, the module 749 dictionary will be cleared when the module falls out of scope even if the 750 dictionary still has live references. To avoid this, copy the dictionary 751 or keep the module around while using its dictionary directly. 741 752 742 753 .. index:: … … 787 798 transformed into an unbound user-defined method object whose :attr:`im_class` 788 799 attribute is :class:`C`. When it would yield a class method object, it is 789 transformed into a bound user-defined method object whose :attr:`im_class`790 and :attr:`im_self` attributes are both:class:`C`. When it would yield a800 transformed into a bound user-defined method object whose 801 :attr:`im_self` attribute is :class:`C`. When it would yield a 791 802 static method object, it is transformed into the object wrapped by the static 792 803 method object. See section :ref:`descriptors` for another way in which … … 812 823 Special attributes: :attr:`__name__` is the class name; :attr:`__module__` is 813 824 the module name in which the class was defined; :attr:`__dict__` is the 814 dictionary containing the class's namespace; :attr:` __bases__` is a tuple815 (possibly empty or a singleton) containing the base classes, in the order of816 their occurrence in the base class list; :attr:`__doc__` is the class's817 documentation string, or None if undefined.825 dictionary containing the class's namespace; :attr:`~class.__bases__` is a 826 tuple (possibly empty or a singleton) containing the base classes, in the 827 order of their occurrence in the base class list; :attr:`__doc__` is the 828 class's documentation string, or None if undefined. 818 829 819 830 Class instances … … 860 871 single: __class__ (instance attribute) 861 872 862 Special attributes: :attr:` __dict__` is the attribute dictionary;863 :attr:` __class__` is the instance's class.873 Special attributes: :attr:`~object.__dict__` is the attribute dictionary; 874 :attr:`~instance.__class__` is the instance's class. 864 875 865 876 Files … … 908 919 objects, code objects are immutable and contain no references (directly or 909 920 indirectly) to mutable objects. 921 922 .. index:: 923 single: co_argcount (code object attribute) 924 single: co_code (code object attribute) 925 single: co_consts (code object attribute) 926 single: co_filename (code object attribute) 927 single: co_firstlineno (code object attribute) 928 single: co_flags (code object attribute) 929 single: co_lnotab (code object attribute) 930 single: co_name (code object attribute) 931 single: co_names (code object attribute) 932 single: co_nlocals (code object attribute) 933 single: co_stacksize (code object attribute) 934 single: co_varnames (code object attribute) 935 single: co_cellvars (code object attribute) 936 single: co_freevars (code object attribute) 910 937 911 938 Special read-only attributes: :attr:`co_name` gives the function name; … … 926 953 :attr:`co_flags` is an integer encoding a number of flags for the interpreter. 927 954 928 .. index::929 single: co_argcount (code object attribute)930 single: co_code (code object attribute)931 single: co_consts (code object attribute)932 single: co_filename (code object attribute)933 single: co_firstlineno (code object attribute)934 single: co_flags (code object attribute)935 single: co_lnotab (code object attribute)936 single: co_name (code object attribute)937 single: co_names (code object attribute)938 single: co_nlocals (code object attribute)939 single: co_stacksize (code object attribute)940 single: co_varnames (code object attribute)941 single: co_cellvars (code object attribute)942 single: co_freevars (code object attribute)943 944 955 .. index:: object: generator 945 956 … … 1060 1071 single: step (slice object attribute) 1061 1072 1062 Special read-only attributes: :attr:` start` is the lower bound; :attr:`stop` is1063 the upper bound; :attr:`step` is the step value; each is ``None`` if omitted.1064 These attributes can have any type.1073 Special read-only attributes: :attr:`~slice.start` is the lower bound; 1074 :attr:`~slice.stop` is the upper bound; :attr:`~slice.step` is the step 1075 value; each is ``None`` if omitted. These attributes can have any type. 1065 1076 1066 1077 Slice objects support one method: … … 1141 1152 single: class; old-style 1142 1153 1143 Old-style classes are removed in Python 3 .0, leaving only the semantics of1154 Old-style classes are removed in Python 3, leaving only the semantics of 1144 1155 new-style classes. 1145 1156 … … 1169 1180 object being modelled. For example, some sequences may work well with retrieval 1170 1181 of individual elements, but extracting a slice may not make sense. (One example 1171 of this is the :class:`NodeList` interface in the W3C's Document Object Model.) 1182 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document 1183 Object Model.) 1172 1184 1173 1185 … … 1274 1286 called. 1275 1287 1288 See also the :option:`-R` command-line option. 1289 1276 1290 1277 1291 .. method:: object.__repr__(self) … … 1354 1368 1355 1369 To automatically generate ordering operations from a single root operation, 1356 see the `Total Ordering recipe in the ASPN cookbook 1357 <http://code.activestate.com/recipes/576529/>`_\. 1370 see :func:`functools.total_ordering`. 1358 1371 1359 1372 .. method:: object.__cmp__(self, other) … … 1532 1545 1533 1546 The following methods only apply when an instance of the class containing the 1534 method (a so-called *descriptor* class) appears in the class dictionary of1535 another new-style class, known as the *owner* class. In the examples below, "the 1536 attribute" refers to the attribute whose name is the key of the property in the 1537 owner class' ``__dict__``. Descriptors can only be implemented as new-style 1538 class es themselves.1547 method (a so-called *descriptor* class) appears in an *owner* class (the 1548 descriptor must be in either the owner's class dictionary or in the class 1549 dictionary for one of its parents). In the examples below, "the attribute" 1550 refers to the attribute whose name is the key of the property in the owner 1551 class' :attr:`__dict__`. 1539 1552 1540 1553 … … 1601 1614 obj).m()`` searches ``obj.__class__.__mro__`` for the base class ``A`` 1602 1615 immediately preceding ``B`` and then invokes the descriptor with the call: 1603 ``A.__dict__['m'].__get__(obj, A)``.1616 ``A.__dict__['m'].__get__(obj, obj.__class__)``. 1604 1617 1605 1618 For instance bindings, the precedence of descriptor invocation depends on the 1606 which descriptor methods are defined. Normally, data descriptors define both 1607 :meth:`__get__` and :meth:`__set__`, while non-data descriptors have just the 1608 :meth:`__get__` method. Data descriptors always override a redefinition in an 1619 which descriptor methods are defined. A descriptor can define any combination 1620 of :meth:`__get__`, :meth:`__set__` and :meth:`__delete__`. If it does not 1621 define :meth:`__get__`, then accessing the attribute will return the descriptor 1622 object itself unless there is a value in the object's instance dictionary. If 1623 the descriptor defines :meth:`__set__` and/or :meth:`__delete__`, it is a data 1624 descriptor; if it defines neither, it is a non-data descriptor. Normally, data 1625 descriptors define both :meth:`__get__` and :meth:`__set__`, while non-data 1626 descriptors have just the :meth:`__get__` method. Data descriptors with 1627 :meth:`__set__` and :meth:`__get__` defined always override a redefinition in an 1609 1628 instance dictionary. In contrast, non-data descriptors can be overridden by 1610 instances. [#]_1629 instances. 1611 1630 1612 1631 Python methods (including :func:`staticmethod` and :func:`classmethod`) are … … 1756 1775 property creation, proxies, frameworks, and automatic resource 1757 1776 locking/synchronization. 1777 1778 1779 Customizing instance and subclass checks 1780 ---------------------------------------- 1781 1782 .. versionadded:: 2.6 1783 1784 The following methods are used to override the default behavior of the 1785 :func:`isinstance` and :func:`issubclass` built-in functions. 1786 1787 In particular, the metaclass :class:`abc.ABCMeta` implements these methods in 1788 order to allow the addition of Abstract Base Classes (ABCs) as "virtual base 1789 classes" to any class or type (including built-in types), including other 1790 ABCs. 1791 1792 .. method:: class.__instancecheck__(self, instance) 1793 1794 Return true if *instance* should be considered a (direct or indirect) 1795 instance of *class*. If defined, called to implement ``isinstance(instance, 1796 class)``. 1797 1798 1799 .. method:: class.__subclasscheck__(self, subclass) 1800 1801 Return true if *subclass* should be considered a (direct or indirect) 1802 subclass of *class*. If defined, called to implement ``issubclass(subclass, 1803 class)``. 1804 1805 1806 Note that these methods are looked up on the type (metaclass) of a class. They 1807 cannot be defined as class methods in the actual class. This is consistent with 1808 the lookup of special methods that are called on instances, only in this 1809 case the instance is itself a class. 1810 1811 .. seealso:: 1812 1813 :pep:`3119` - Introducing Abstract Base Classes 1814 Includes the specification for customizing :func:`isinstance` and 1815 :func:`issubclass` behavior through :meth:`~class.__instancecheck__` and 1816 :meth:`~class.__subclasscheck__`, with motivation for this functionality 1817 in the context of adding Abstract Base Classes (see the :mod:`abc` 1818 module) to the language. 1758 1819 1759 1820 … … 1788 1849 :meth:`values`, :meth:`items`, :meth:`has_key`, :meth:`get`, :meth:`clear`, 1789 1850 :meth:`setdefault`, :meth:`iterkeys`, :meth:`itervalues`, :meth:`iteritems`, 1790 :meth:`pop`, :meth:`popitem`, :meth:` copy`, and :meth:`update` behaving similar1851 :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and :meth:`update` behaving similar 1791 1852 to those for Python's standard dictionary objects. The :mod:`UserDict` module 1792 1853 provides a :class:`DictMixin` class to help create those methods from a base set … … 2178 2239 evolved, the coercion rules have become hard to document precisely; documenting 2179 2240 what one version of one particular implementation does is undesirable. Instead, 2180 here are some informal guidelines regarding coercion. In Python 3 .0, coercion2241 here are some informal guidelines regarding coercion. In Python 3, coercion 2181 2242 will not be supported. 2182 2243 … … 2253 2314 * 2254 2315 2255 In ``x * y``, if one opera toris a sequence that implements sequence2316 In ``x * y``, if one operand is a sequence that implements sequence 2256 2317 repetition, and the other is an integer (:class:`int` or :class:`long`), 2257 2318 sequence repetition is invoked. … … 2266 2327 2267 2328 In the current implementation, the built-in numeric types :class:`int`, 2268 :class:`long` and :class:`float` do not use coercion; the type :class:`complex` 2269 however does use coercion for binary operators and rich comparisons, despite 2270 the above rules. The difference can become apparent when subclassing these 2271 types. Over time, the type :class:`complex` may be fixed to avoid coercion. 2329 :class:`long`, :class:`float`, and :class:`complex` do not use coercion. 2272 2330 All these types implement a :meth:`__coerce__` method, for use by the built-in 2273 2331 :func:`coerce` function. 2332 2333 .. versionchanged:: 2.7 2334 2335 The complex type no longer makes implicit calls to the :meth:`__coerce__` 2336 method for mixed-type binary arithmetic operations. 2274 2337 2275 2338 … … 2435 2498 lead to some very strange behaviour if it is handled incorrectly. 2436 2499 2437 .. [#] A descriptor can define any combination of :meth:`__get__`,2438 :meth:`__set__` and :meth:`__delete__`. If it does not define :meth:`__get__`,2439 then accessing the attribute even on an instance will return the descriptor2440 object itself. If the descriptor defines :meth:`__set__` and/or2441 :meth:`__delete__`, it is a data descriptor; if it defines neither, it is a2442 non-data descriptor.2443 2444 2500 .. [#] For operands of the same type, it is assumed that if the non-reflected method 2445 2501 (such as :meth:`__add__`) fails the operation is not supported, which is why the
Note:
See TracChangeset
for help on using the changeset viewer.