Kaydet (Commit) 85eb8c10 authored tarafından Georg Brandl's avatar Georg Brandl

- document bytes()

- throw out many mentions of "old-style/new-style"
- add memoryview() though I somebody has to fill in the details
- throw out str.decode()
- throw out classobj and instanceobj
üst 3540ef16
...@@ -2503,43 +2503,6 @@ Dictionary Objects ...@@ -2503,43 +2503,6 @@ Dictionary Objects
Other Objects Other Objects
============= =============
.. _classobjects:
Class Objects
-------------
.. index:: object: class
Note that the class objects described here represent old-style classes, which
will go away in Python 3. When creating new types for extension modules, you
will want to work with type objects (section :ref:`typeobjects`).
.. ctype:: PyClassObject
The C structure of the objects used to describe built-in classes.
.. cvar:: PyObject* PyClass_Type
.. index:: single: ClassType (in module types)
This is the type object for class objects; it is the same object as
``types.ClassType`` in the Python layer.
.. cfunction:: int PyClass_Check(PyObject *o)
Return true if the object *o* is a class object, including instances of types
derived from the standard class object. Return false in all other cases.
.. cfunction:: int PyClass_IsSubclass(PyObject *klass, PyObject *base)
Return true if *klass* is a subclass of *base*. Return false in all other cases.
.. _fileobjects: .. _fileobjects:
File Objects File Objects
...@@ -2668,40 +2631,6 @@ change in future releases of Python. ...@@ -2668,40 +2631,6 @@ change in future releases of Python.
failure; the appropriate exception will be set. failure; the appropriate exception will be set.
.. _instanceobjects:
Instance Objects
----------------
.. index:: object: instance
There are very few functions specific to instance objects.
.. cvar:: PyTypeObject PyInstance_Type
Type object for class instances.
.. cfunction:: int PyInstance_Check(PyObject *obj)
Return true if *obj* is an instance.
.. cfunction:: PyObject* PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)
Create a new instance of a specific class. The parameters *arg* and *kw* are
used as the positional and keyword parameters to the object's constructor.
.. cfunction:: PyObject* PyInstance_NewRaw(PyObject *class, PyObject *dict)
Create a new instance of a specific class without calling its constructor.
*class* is the class of new object. The *dict* parameter will be used as the
object's :attr:`__dict__`; if *NULL*, a new dictionary will be created for the
instance.
.. _function-objects: .. _function-objects:
Function Objects Function Objects
......
...@@ -750,6 +750,7 @@ return true, otherwise they return false and raise an appropriate exception. ...@@ -750,6 +750,7 @@ return true, otherwise they return false and raise an appropriate exception.
va_list rather than a variable number of arguments. va_list rather than a variable number of arguments.
.. XXX deprecated, will be removed
.. cfunction:: int PyArg_Parse(PyObject *args, const char *format, ...) .. cfunction:: int PyArg_Parse(PyObject *args, const char *format, ...)
Function used to deconstruct the argument lists of "old-style" functions --- Function used to deconstruct the argument lists of "old-style" functions ---
......
...@@ -29,8 +29,8 @@ Glossary ...@@ -29,8 +29,8 @@ Glossary
bytecode. bytecode.
classic class classic class
Any class which does not inherit from :class:`object`. See One of the two flavors of classes in earlier Python versions. Since
:term:`new-style class`. Python 3.0, there are no classic classes anymore.
coercion coercion
The implicit conversion of an instance of one type to another during an The implicit conversion of an instance of one type to another during an
...@@ -58,15 +58,14 @@ Glossary ...@@ -58,15 +58,14 @@ Glossary
it's almost certain you can safely ignore them. it's almost certain you can safely ignore them.
descriptor descriptor
Any *new-style* object that defines the methods :meth:`__get__`, An object that defines the methods :meth:`__get__`, :meth:`__set__`, or
:meth:`__set__`, or :meth:`__delete__`. When a class attribute is a :meth:`__delete__`. When a class attribute is a descriptor, its special
descriptor, its special binding behavior is triggered upon attribute binding behavior is triggered upon attribute lookup. Normally, writing
lookup. Normally, writing *a.b* looks up the object *b* in the class *a.b* looks up the object *b* in the class dictionary for *a*, but if *b*
dictionary for *a*, but if *b* is a descriptor, the defined method gets is a descriptor, the defined method gets called. Understanding
called. Understanding descriptors is a key to a deep understanding of descriptors is a key to a deep understanding of Python because they are
Python because they are the basis for many features including functions, the basis for many features including functions, methods, properties,
methods, properties, class methods, static methods, and reference to super class methods, static methods, and reference to super classes.
classes.
dictionary dictionary
An associative array, where arbitrary keys are mapped to values. The use An associative array, where arbitrary keys are mapped to values. The use
...@@ -277,11 +276,10 @@ Glossary ...@@ -277,11 +276,10 @@ Glossary
scope. Likewise, global variables read and write to the global namespace. scope. Likewise, global variables read and write to the global namespace.
new-style class new-style class
Any class that inherits from :class:`object`. This includes all built-in Old name for the flavor of classes now used for all class objects. In
types like :class:`list` and :class:`dict`. Only new-style classes can earlier Python versions, only new-style classes could use Python's newer,
use Python's newer, versatile features like :attr:`__slots__`, versatile features like :attr:`__slots__`, descriptors, properties,
descriptors, properties, :meth:`__getattribute__`, class methods, and :meth:`__getattribute__`, class methods, and static methods.
static methods.
Python 3000 Python 3000
Nickname for the next major Python version, 3.0 (coined long ago when the Nickname for the next major Python version, 3.0 (coined long ago when the
...@@ -294,11 +292,11 @@ Glossary ...@@ -294,11 +292,11 @@ Glossary
implementation level to keep track of allocated memory. implementation level to keep track of allocated memory.
__slots__ __slots__
A declaration inside a :term:`new-style class` that saves memory by A declaration inside a class that saves memory by pre-declaring space for
pre-declaring space for instance attributes and eliminating instance instance attributes and eliminating instance dictionaries. Though
dictionaries. Though popular, the technique is somewhat tricky to get popular, the technique is somewhat tricky to get right and is best
right and is best reserved for rare cases where there are large numbers of reserved for rare cases where there are large numbers of instances in a
instances in a memory-critical application. memory-critical application.
sequence sequence
An :term:`iterable` which supports efficient element access using integer An :term:`iterable` which supports efficient element access using integer
......
...@@ -139,12 +139,37 @@ available. They are listed here in alphabetical order. ...@@ -139,12 +139,37 @@ available. They are listed here in alphabetical order.
If no argument is given, this function returns :const:`False`. If no argument is given, this function returns :const:`False`.
.. function:: bytes([arg[, encoding[, errors]]])
Return a new array of bytes. The :class:`bytes` type is a mutable sequence
of integers in the range 0 <= x < 256. It has most of the usual methods of
mutable sequences, described in :ref:`typesseq-mutable`, as well as a few
methods borrowed from strings, described in :ref:`bytes-methods`.
The optional *arg* parameter can be used to initialize the array in a few
different ways:
* If it is a *string*, you must also give the *encoding* (and optionally,
*errors*) parameters; :func:`bytes` then acts like :meth:`str.encode`.
* If it is an *integer*, the array will have that size and will be
initialized with null bytes.
* If it is an object conforming to the *buffer* interface, a read-only buffer
of the object will be used to initialize the bytes array.
* If it is an *iterable*, it must be an iterable of integers in the range 0
<= x < 256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
.. function:: chr(i) .. function:: chr(i)
Return the string of one character whose Unicode codepoint is the integer *i*. For Return the string of one character whose Unicode codepoint is the integer
example, ``chr(97)`` returns the string ``'a'``. This is the inverse of *i*. For example, ``chr(97)`` returns the string ``'a'``. This is the
:func:`ord`. The valid range for the argument depends how Python was inverse of :func:`ord`. The valid range for the argument depends how Python
configured -- it may be either UCS2 [0..0xFFFF] or UCS4 [0..0x10FFFF]. was configured -- it may be either UCS2 [0..0xFFFF] or UCS4 [0..0x10FFFF].
:exc:`ValueError` will be raised if *i* is outside that range. :exc:`ValueError` will be raised if *i* is outside that range.
...@@ -557,15 +582,13 @@ available. They are listed here in alphabetical order. ...@@ -557,15 +582,13 @@ available. They are listed here in alphabetical order.
.. function:: isinstance(object, classinfo) .. function:: isinstance(object, classinfo)
Return true if the *object* argument is an instance of the *classinfo* argument, Return true if the *object* argument is an instance of the *classinfo*
or of a (direct or indirect) subclass thereof. Also return true if *classinfo* argument, or of a (direct or indirect) subclass thereof. If *object* is not
is a type object (new-style class) and *object* is an object of that type or of an object of the given type, the function always returns false. If
a (direct or indirect) subclass thereof. If *object* is not a class instance or *classinfo* is not a class (type object), it may be a tuple of type objects,
an object of the given type, the function always returns false. If *classinfo* or may recursively contain other such tuples (other sequence types are not
is neither a class object nor a type object, it may be a tuple of class or type accepted). If *classinfo* is not a type or tuple of types and such tuples,
objects, or may recursively contain other such tuples (other sequence types are a :exc:`TypeError` exception is raised.
not accepted). If *classinfo* is not a class, type, or tuple of classes, types,
and such tuples, a :exc:`TypeError` exception is raised.
.. versionchanged:: 2.2 .. versionchanged:: 2.2
Support for a tuple of type information was added. Support for a tuple of type information was added.
...@@ -659,6 +682,13 @@ available. They are listed here in alphabetical order. ...@@ -659,6 +682,13 @@ available. They are listed here in alphabetical order.
Added support for the optional *key* argument. Added support for the optional *key* argument.
.. function:: memoryview(obj)
Return a "memory view" object created from the given argument.
XXX: To be documented.
.. function:: min(iterable[, args...][key]) .. function:: min(iterable[, args...][key])
With a single argument *iterable*, return the smallest item of a non-empty With a single argument *iterable*, return the smallest item of a non-empty
...@@ -682,9 +712,13 @@ available. They are listed here in alphabetical order. ...@@ -682,9 +712,13 @@ available. They are listed here in alphabetical order.
.. function:: object() .. function:: object()
Return a new featureless object. :class:`object` is a base for all new style Return a new featureless object. :class:`object` is a base for all classes.
classes. It has the methods that are common to all instances of new style It has the methods that are common to all instances of Python classes.
classes.
.. note::
:class:`object` does *not* have a :attr:`__dict__`, so you can't assign
arbitrary attributes to an instance of the :class:`object` class.
.. versionadded:: 2.2 .. versionadded:: 2.2
...@@ -797,8 +831,7 @@ available. They are listed here in alphabetical order. ...@@ -797,8 +831,7 @@ available. They are listed here in alphabetical order.
.. function:: property([fget[, fset[, fdel[, doc]]]]) .. function:: property([fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that derive from Return a property attribute.
:class:`object`).
*fget* is a function for getting an attribute value, likewise *fset* is a *fget* is a function for getting an attribute value, likewise *fset* is a
function for setting, and *fdel* a function for del'ing, an attribute. Typical function for setting, and *fdel* a function for del'ing, an attribute. Typical
...@@ -1023,11 +1056,12 @@ available. They are listed here in alphabetical order. ...@@ -1023,11 +1056,12 @@ available. They are listed here in alphabetical order.
.. function:: super(type[, object-or-type]) .. function:: super(type[, object-or-type])
.. XXX need to document PEP "new super"
Return the superclass of *type*. If the second argument is omitted the super Return the superclass of *type*. If the second argument is omitted the super
object returned is unbound. If the second argument is an object, object returned is unbound. If the second argument is an object,
``isinstance(obj, type)`` must be true. If the second argument is a type, ``isinstance(obj, type)`` must be true. If the second argument is a type,
``issubclass(type2, type)`` must be true. :func:`super` only works for new-style ``issubclass(type2, type)`` must be true.
classes.
A typical use for calling a cooperative superclass method is:: A typical use for calling a cooperative superclass method is::
...@@ -1061,23 +1095,26 @@ available. They are listed here in alphabetical order. ...@@ -1061,23 +1095,26 @@ available. They are listed here in alphabetical order.
.. index:: object: type .. index:: object: type
Return the type of an *object*. The return value is a type object. The Return the type of an *object*. The return value is a type object and
:func:`isinstance` built-in function is recommended for testing the type of an generally the same object as returned by ``object.__class__``.
object.
The :func:`isinstance` built-in function is recommended for testing the type
of an object, because it takes subclasses into account.
With three arguments, :func:`type` functions as a constructor as detailed below. With three arguments, :func:`type` functions as a constructor as detailed
below.
.. function:: type(name, bases, dict) .. function:: type(name, bases, dict)
:noindex: :noindex:
Return a new type object. This is essentially a dynamic form of the Return a new type object. This is essentially a dynamic form of the
:keyword:`class` statement. The *name* string is the class name and becomes the :keyword:`class` statement. The *name* string is the class name and becomes
:attr:`__name__` attribute; the *bases* tuple itemizes the base classes and the :attr:`__name__` attribute; the *bases* tuple itemizes the base classes
becomes the :attr:`__bases__` attribute; and the *dict* dictionary is the and becomes the :attr:`__bases__` attribute; and the *dict* dictionary is the
namespace containing definitions for class body and becomes the :attr:`__dict__` namespace containing definitions for class body and becomes the
attribute. For example, the following two statements create identical :attr:`__dict__` attribute. For example, the following two statements create
:class:`type` objects:: identical :class:`type` objects::
>>> class X(object): >>> class X(object):
... a = 1 ... a = 1
...@@ -1128,6 +1165,7 @@ Python programmers, trainers, students and bookwriters should feel free to ...@@ -1128,6 +1165,7 @@ Python programmers, trainers, students and bookwriters should feel free to
bypass these functions without concerns about missing something important. bypass these functions without concerns about missing something important.
.. XXX does this go away?
.. function:: buffer(object[, offset[, size]]) .. function:: buffer(object[, offset[, size]])
The *object* argument must be an object that supports the buffer call interface The *object* argument must be an object that supports the buffer call interface
......
...@@ -416,6 +416,8 @@ Pickling and unpickling normal class instances ...@@ -416,6 +416,8 @@ Pickling and unpickling normal class instances
single: __getinitargs__() (copy protocol) single: __getinitargs__() (copy protocol)
single: __init__() (instance constructor) single: __init__() (instance constructor)
.. XXX is __getinitargs__ only used with old-style classes?
When a pickled class instance is unpickled, its :meth:`__init__` method is When a pickled class instance is unpickled, its :meth:`__init__` method is
normally *not* invoked. If it is desirable that the :meth:`__init__` method be normally *not* invoked. If it is desirable that the :meth:`__init__` method be
called on unpickling, an old-style class can define a method called on unpickling, an old-style class can define a method
......
...@@ -547,11 +547,6 @@ Registering an adapter callable ...@@ -547,11 +547,6 @@ Registering an adapter callable
The other possibility is to create a function that converts the type to the The other possibility is to create a function that converts the type to the
string representation and register the function with :meth:`register_adapter`. string representation and register the function with :meth:`register_adapter`.
.. note::
The type/class to adapt must be a new-style class, i. e. it must have
:class:`object` as one of its bases.
.. literalinclude:: ../includes/sqlite3/adapter_point_2.py .. literalinclude:: ../includes/sqlite3/adapter_point_2.py
The :mod:`sqlite3` module has two default adapters for Python's built-in The :mod:`sqlite3` module has two default adapters for Python's built-in
......
...@@ -682,22 +682,6 @@ the :mod:`re` module for string functions based on regular expressions. ...@@ -682,22 +682,6 @@ the :mod:`re` module for string functions based on regular expressions.
slice notation. slice notation.
.. XXX what about str.decode???
.. method:: str.decode([encoding[, errors]])
Decode the string using the codec registered for *encoding*. *encoding*
defaults to the default string encoding. *errors* may be given to set a
different error handling scheme. The default is ``'strict'``, meaning that
encoding errors raise :exc:`UnicodeError`. Other possible values are
``'ignore'``, ``'replace'`` and any other name registered via
:func:`codecs.register_error`, see section :ref:`codec-base-classes`.
.. versionadded:: 2.2
.. versionchanged:: 2.3
Support for other error handling schemes added.
.. method:: str.encode([encoding[, errors]]) .. method:: str.encode([encoding[, errors]])
Return an encoded version of the string. Default encoding is the current Return an encoded version of the string. Default encoding is the current
......
...@@ -540,8 +540,10 @@ must be given a value in the :meth:`__init__` method or in another method. Both ...@@ -540,8 +540,10 @@ must be given a value in the :meth:`__init__` method or in another method. Both
class and instance variables are accessible through the notation class and instance variables are accessible through the notation
"``self.name``", and an instance variable hides a class variable with the same "``self.name``", and an instance variable hides a class variable with the same
name when accessed in this way. Class variables with immutable values can be name when accessed in this way. Class variables with immutable values can be
used as defaults for instance variables. For new-style classes, descriptors can used as defaults for instance variables. Descriptors can be used to create
be used to create instance variables with different implementation details. instance variables with different implementation details.
.. XXX add link to descriptor docs above
.. rubric:: Footnotes .. rubric:: Footnotes
......
This diff is collapsed.
...@@ -483,36 +483,27 @@ definition with multiple base classes looks like this:: ...@@ -483,36 +483,27 @@ definition with multiple base classes looks like this::
. .
<statement-N> <statement-N>
For old-style classes, the only rule is depth-first, left-to-right. Thus, if an Formerly, the only rule was depth-first, left-to-right. Thus, if an attribute
attribute is not found in :class:`DerivedClassName`, it is searched in was not found in :class:`DerivedClassName`, it was searched in :class:`Base1`,
:class:`Base1`, then (recursively) in the base classes of :class:`Base1`, and then (recursively) in the base classes of :class:`Base1`, and only if it was not
only if it is not found there, it is searched in :class:`Base2`, and so on. found there, it was searched in :class:`Base2`, and so on.
(To some people breadth first --- searching :class:`Base2` and :class:`Base3` In the meantime, the method resolution order changes dynamically to support
before the base classes of :class:`Base1` --- looks more natural. However, this cooperative calls to :func:`super`. This approach is known in some other
would require you to know whether a particular attribute of :class:`Base1` is multiple-inheritance languages as call-next-method and is more powerful than the
actually defined in :class:`Base1` or in one of its base classes before you can super call found in single-inheritance languages.
figure out the consequences of a name conflict with an attribute of
:class:`Base2`. The depth-first rule makes no differences between direct and Dynamic ordering is necessary because all cases of multiple inheritance exhibit
inherited attributes of :class:`Base1`.) one or more diamond relationships (where one at least one of the parent classes
can be accessed through multiple paths from the bottommost class). For example,
For new-style classes, the method resolution order changes dynamically to all classes inherit from :class:`object`, so any case of multiple inheritance
support cooperative calls to :func:`super`. This approach is known in some provides more than one path to reach :class:`object`. To keep the base classes
other multiple-inheritance languages as call-next-method and is more powerful from being accessed more than once, the dynamic algorithm linearizes the search
than the super call found in single-inheritance languages. order in a way that preserves the left-to-right ordering specified in each
class, that calls each parent only once, and that is monotonic (meaning that a
With new-style classes, dynamic ordering is necessary because all cases of class can be subclassed without affecting the precedence order of its parents).
multiple inheritance exhibit one or more diamond relationships (where one at Taken together, these properties make it possible to design reliable and
least one of the parent classes can be accessed through multiple paths from the extensible classes with multiple inheritance. For more detail, see
bottommost class). For example, all new-style classes inherit from
:class:`object`, so any case of multiple inheritance provides more than one path
to reach :class:`object`. To keep the base classes from being accessed more
than once, the dynamic algorithm linearizes the search order in a way that
preserves the left-to-right ordering specified in each class, that calls each
parent only once, and that is monotonic (meaning that a class can be subclassed
without affecting the precedence order of its parents). Taken together, these
properties make it possible to design reliable and extensible classes with
multiple inheritance. For more detail, see
http://www.python.org/download/releases/2.3/mro/. http://www.python.org/download/releases/2.3/mro/.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment