Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
65e5f80c
Commit
65e5f80c
authored
Aug 02, 2010
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#6928: fix class docs w.r.t. new metaclasses.
parent
502c3eb5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
26 deletions
+24
-26
Doc/reference/compound_stmts.rst
Doc/reference/compound_stmts.rst
+24
-26
No files found.
Doc/reference/compound_stmts.rst
View file @
65e5f80c
...
...
@@ -552,24 +552,27 @@ Class definitions
A class definition defines a class object (see section :ref:`types`):
.. XXX need to document PEP 3115 changes here (new metaclasses)
.. productionlist::
classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite`
inheritance: "(" [`
expression_list`
] ")"
inheritance: "(" [`
argument_list` [","]
] ")"
classname: `identifier`
A class definition is an executable statement. It first evaluates the
inheritance list, if present. Each item in the inheritance list should evaluate
to a class object or class type which allows subclassing. The class's suite is
then executed in a new execution frame (see section :ref:`naming`), using a
newly created local namespace and the original global namespace. (Usually, the
suite contains only function definitions.) When the class's suite finishes
execution, its execution frame is discarded but its local namespace is
saved. [#]_ A class object is then created using the inheritance list for the
base classes and the saved local namespace for the attribute dictionary. The
class name is bound to this class object in the original local namespace.
A class definition is an executable statement. The inheritance list usually
gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so
each item in the list should evaluate to a class object which allows
subclassing.
The class's suite is then executed in a new execution frame (see :ref:`naming`),
using a newly created local namespace and the original global namespace.
(Usually, the suite contains mostly function definitions.) When the class's
suite finishes execution, its execution frame is discarded but its local
namespace is saved. [#]_ A class object is then created using the inheritance
list for the base classes and the saved local namespace for the attribute
dictionary. The class name is bound to this class object in the original local
namespace.
Class creation can be customized heavily using :ref:`metaclasses <metaclasses>`.
Classes can also be decorated; as with functions, ::
...
...
@@ -583,25 +586,20 @@ is equivalent to ::
Foo = f1(arg)(f2(Foo))
**Programmer's note:** Variables defined in the class definition are class
variables; they are shared by instances. Instance variabl
es can be set in a
method with ``self.name = value``. Both class and instance
variabl
es are
accessible through the notation "``self.name``", and an instance
variabl
e hides
a class
variable with the same name when accessed in this way. Class variable
s
can be used as defaults for instance variables, but using mutable values ther
e
can lead to unexpected results. Descriptors can be used to create instance
variables with different implementation details.
attributes; they are shared by instances. Instance attribut
es can be set in a
method with ``self.name = value``. Both class and instance
attribut
es are
accessible through the notation "``self.name``", and an instance
attribut
e hides
a class
attribute with the same name when accessed in this way. Clas
s
attributes can be used as defaults for instance attributes, but using mutabl
e
values there can lead to unexpected results. :ref:`Descriptors <descriptors>`
can be used to create instance
variables with different implementation details.
.. XXX add link to descriptor docs above
.. seealso::
:pep:`3116` - Metaclasses in Python 3
:pep:`3129` - Class Decorators
Class definitions, like function definitions, may be wrapped by one or more
:term:`decorator` expressions. The evaluation rules for the decorator
expressions are the same as for functions. The result must be a class object,
which is then bound to the class name.
.. rubric:: Footnotes
...
...
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