blob: 2cc3610c22036bb8ea4567311124b0e84d8b7f51 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-classes:
2
3*******
4Classes
5*******
6
Georg Brandlb8d0e362010-11-26 07:53:50 +00007Compared with other programming languages, Python's class mechanism adds classes
8with a minimum of new syntax and semantics. It is a mixture of the class
9mechanisms found in C++ and Modula-3. Python classes provide all the standard
10features of Object Oriented Programming: the class inheritance mechanism allows
Georg Brandl8ec7f652007-08-15 14:28:01 +000011multiple base classes, a derived class can override any methods of its base
12class or classes, and a method can call the method of a base class with the same
Georg Brandlb8d0e362010-11-26 07:53:50 +000013name. Objects can contain arbitrary amounts and kinds of data. As is true for
14modules, classes partake of the dynamic nature of Python: they are created at
15runtime, and can be modified further after creation.
Georg Brandl8ec7f652007-08-15 14:28:01 +000016
Georg Brandlb8d0e362010-11-26 07:53:50 +000017In C++ terminology, normally class members (including the data members) are
18*public* (except see below :ref:`tut-private`), and all member functions are
19*virtual*. As in Modula-3, there are no shorthands for referencing the object's
20members from its methods: the method function is declared with an explicit first
21argument representing the object, which is provided implicitly by the call. As
22in Smalltalk, classes themselves are objects. This provides semantics for
23importing and renaming. Unlike C++ and Modula-3, built-in types can be used as
24base classes for extension by the user. Also, like in C++, most built-in
25operators with special syntax (arithmetic operators, subscripting etc.) can be
26redefined for class instances.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
Georg Brandl4938fef2009-07-29 17:50:25 +000028(Lacking universally accepted terminology to talk about classes, I will make
29occasional use of Smalltalk and C++ terms. I would use Modula-3 terms, since
Georg Brandl8ec7f652007-08-15 14:28:01 +000030its object-oriented semantics are closer to those of Python than C++, but I
31expect that few readers have heard of it.)
32
Georg Brandl4938fef2009-07-29 17:50:25 +000033
34.. _tut-object:
35
36A Word About Names and Objects
37==============================
38
Georg Brandl8ec7f652007-08-15 14:28:01 +000039Objects have individuality, and multiple names (in multiple scopes) can be bound
40to the same object. This is known as aliasing in other languages. This is
41usually not appreciated on a first glance at Python, and can be safely ignored
42when dealing with immutable basic types (numbers, strings, tuples). However,
Georg Brandl4938fef2009-07-29 17:50:25 +000043aliasing has a possibly surprising effect on the semantics of Python code
44involving mutable objects such as lists, dictionaries, and most other types.
45This is usually used to the benefit of the program, since aliases behave like
46pointers in some respects. For example, passing an object is cheap since only a
47pointer is passed by the implementation; and if a function modifies an object
48passed as an argument, the caller will see the change --- this eliminates the
49need for two different argument passing mechanisms as in Pascal.
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51
52.. _tut-scopes:
53
Georg Brandl0cfbd652009-08-24 17:20:40 +000054Python Scopes and Namespaces
55============================
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
57Before introducing classes, I first have to tell you something about Python's
58scope rules. Class definitions play some neat tricks with namespaces, and you
59need to know how scopes and namespaces work to fully understand what's going on.
60Incidentally, knowledge about this subject is useful for any advanced Python
61programmer.
62
63Let's begin with some definitions.
64
65A *namespace* is a mapping from names to objects. Most namespaces are currently
66implemented as Python dictionaries, but that's normally not noticeable in any
67way (except for performance), and it may change in the future. Examples of
Georg Brandl7d4bfb32010-08-02 21:44:25 +000068namespaces are: the set of built-in names (containing functions such as :func:`abs`, and
Georg Brandl8ec7f652007-08-15 14:28:01 +000069built-in exception names); the global names in a module; and the local names in
70a function invocation. In a sense the set of attributes of an object also form
71a namespace. The important thing to know about namespaces is that there is
72absolutely no relation between names in different namespaces; for instance, two
Georg Brandl4938fef2009-07-29 17:50:25 +000073different modules may both define a function ``maximize`` without confusion ---
Georg Brandl8ec7f652007-08-15 14:28:01 +000074users of the modules must prefix it with the module name.
75
76By the way, I use the word *attribute* for any name following a dot --- for
77example, in the expression ``z.real``, ``real`` is an attribute of the object
78``z``. Strictly speaking, references to names in modules are attribute
79references: in the expression ``modname.funcname``, ``modname`` is a module
80object and ``funcname`` is an attribute of it. In this case there happens to be
81a straightforward mapping between the module's attributes and the global names
82defined in the module: they share the same namespace! [#]_
83
84Attributes may be read-only or writable. In the latter case, assignment to
85attributes is possible. Module attributes are writable: you can write
86``modname.the_answer = 42``. Writable attributes may also be deleted with the
87:keyword:`del` statement. For example, ``del modname.the_answer`` will remove
88the attribute :attr:`the_answer` from the object named by ``modname``.
89
Georg Brandl0cfbd652009-08-24 17:20:40 +000090Namespaces are created at different moments and have different lifetimes. The
Georg Brandl8ec7f652007-08-15 14:28:01 +000091namespace containing the built-in names is created when the Python interpreter
92starts up, and is never deleted. The global namespace for a module is created
93when the module definition is read in; normally, module namespaces also last
94until the interpreter quits. The statements executed by the top-level
95invocation of the interpreter, either read from a script file or interactively,
96are considered part of a module called :mod:`__main__`, so they have their own
97global namespace. (The built-in names actually also live in a module; this is
98called :mod:`__builtin__`.)
99
100The local namespace for a function is created when the function is called, and
101deleted when the function returns or raises an exception that is not handled
102within the function. (Actually, forgetting would be a better way to describe
103what actually happens.) Of course, recursive invocations each have their own
104local namespace.
105
106A *scope* is a textual region of a Python program where a namespace is directly
107accessible. "Directly accessible" here means that an unqualified reference to a
108name attempts to find the name in the namespace.
109
110Although scopes are determined statically, they are used dynamically. At any
111time during execution, there are at least three nested scopes whose namespaces
Georg Brandl4938fef2009-07-29 17:50:25 +0000112are directly accessible:
113
114* the innermost scope, which is searched first, contains the local names
115* the scopes of any enclosing functions, which are searched starting with the
116 nearest enclosing scope, contains non-local, but also non-global names
117* the next-to-last scope contains the current module's global names
118* the outermost scope (searched last) is the namespace containing built-in names
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
120If a name is declared global, then all references and assignments go directly to
121the middle scope containing the module's global names. Otherwise, all variables
122found outside of the innermost scope are read-only (an attempt to write to such
123a variable will simply create a *new* local variable in the innermost scope,
124leaving the identically named outer variable unchanged).
125
126Usually, the local scope references the local names of the (textually) current
127function. Outside functions, the local scope references the same namespace as
128the global scope: the module's namespace. Class definitions place yet another
129namespace in the local scope.
130
131It is important to realize that scopes are determined textually: the global
132scope of a function defined in a module is that module's namespace, no matter
133from where or by what alias the function is called. On the other hand, the
134actual search for names is done dynamically, at run time --- however, the
135language definition is evolving towards static name resolution, at "compile"
136time, so don't rely on dynamic name resolution! (In fact, local variables are
137already determined statically.)
138
Georg Brandl4938fef2009-07-29 17:50:25 +0000139A special quirk of Python is that -- if no :keyword:`global` statement is in
140effect -- assignments to names always go into the innermost scope. Assignments
141do not copy data --- they just bind names to objects. The same is true for
142deletions: the statement ``del x`` removes the binding of ``x`` from the
143namespace referenced by the local scope. In fact, all operations that introduce
144new names use the local scope: in particular, :keyword:`import` statements and
145function definitions bind the module or function name in the local scope. (The
146:keyword:`global` statement can be used to indicate that particular variables
147live in the global scope.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
149
150.. _tut-firstclasses:
151
152A First Look at Classes
153=======================
154
155Classes introduce a little bit of new syntax, three new object types, and some
156new semantics.
157
158
159.. _tut-classdefinition:
160
161Class Definition Syntax
162-----------------------
163
164The simplest form of class definition looks like this::
165
166 class ClassName:
167 <statement-1>
168 .
169 .
170 .
171 <statement-N>
172
173Class definitions, like function definitions (:keyword:`def` statements) must be
174executed before they have any effect. (You could conceivably place a class
175definition in a branch of an :keyword:`if` statement, or inside a function.)
176
177In practice, the statements inside a class definition will usually be function
178definitions, but other statements are allowed, and sometimes useful --- we'll
179come back to this later. The function definitions inside a class normally have
180a peculiar form of argument list, dictated by the calling conventions for
181methods --- again, this is explained later.
182
183When a class definition is entered, a new namespace is created, and used as the
184local scope --- thus, all assignments to local variables go into this new
185namespace. In particular, function definitions bind the name of the new
186function here.
187
188When a class definition is left normally (via the end), a *class object* is
189created. This is basically a wrapper around the contents of the namespace
190created by the class definition; we'll learn more about class objects in the
191next section. The original local scope (the one in effect just before the class
192definition was entered) is reinstated, and the class object is bound here to the
193class name given in the class definition header (:class:`ClassName` in the
194example).
195
196
197.. _tut-classobjects:
198
199Class Objects
200-------------
201
202Class objects support two kinds of operations: attribute references and
203instantiation.
204
205*Attribute references* use the standard syntax used for all attribute references
206in Python: ``obj.name``. Valid attribute names are all the names that were in
207the class's namespace when the class object was created. So, if the class
208definition looked like this::
209
210 class MyClass:
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000211 """A simple example class"""
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212 i = 12345
213 def f(self):
214 return 'hello world'
215
216then ``MyClass.i`` and ``MyClass.f`` are valid attribute references, returning
217an integer and a function object, respectively. Class attributes can also be
218assigned to, so you can change the value of ``MyClass.i`` by assignment.
219:attr:`__doc__` is also a valid attribute, returning the docstring belonging to
220the class: ``"A simple example class"``.
221
222Class *instantiation* uses function notation. Just pretend that the class
223object is a parameterless function that returns a new instance of the class.
224For example (assuming the above class)::
225
226 x = MyClass()
227
228creates a new *instance* of the class and assigns this object to the local
229variable ``x``.
230
231The instantiation operation ("calling" a class object) creates an empty object.
232Many classes like to create objects with instances customized to a specific
233initial state. Therefore a class may define a special method named
234:meth:`__init__`, like this::
235
236 def __init__(self):
237 self.data = []
238
239When a class defines an :meth:`__init__` method, class instantiation
240automatically invokes :meth:`__init__` for the newly-created class instance. So
241in this example, a new, initialized instance can be obtained by::
242
243 x = MyClass()
244
245Of course, the :meth:`__init__` method may have arguments for greater
246flexibility. In that case, arguments given to the class instantiation operator
247are passed on to :meth:`__init__`. For example, ::
248
249 >>> class Complex:
250 ... def __init__(self, realpart, imagpart):
251 ... self.r = realpart
252 ... self.i = imagpart
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000253 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000254 >>> x = Complex(3.0, -4.5)
255 >>> x.r, x.i
256 (3.0, -4.5)
257
258
259.. _tut-instanceobjects:
260
261Instance Objects
262----------------
263
264Now what can we do with instance objects? The only operations understood by
265instance objects are attribute references. There are two kinds of valid
266attribute names, data attributes and methods.
267
268*data attributes* correspond to "instance variables" in Smalltalk, and to "data
269members" in C++. Data attributes need not be declared; like local variables,
270they spring into existence when they are first assigned to. For example, if
271``x`` is the instance of :class:`MyClass` created above, the following piece of
272code will print the value ``16``, without leaving a trace::
273
274 x.counter = 1
275 while x.counter < 10:
276 x.counter = x.counter * 2
277 print x.counter
278 del x.counter
279
280The other kind of instance attribute reference is a *method*. A method is a
281function that "belongs to" an object. (In Python, the term method is not unique
282to class instances: other object types can have methods as well. For example,
283list objects have methods called append, insert, remove, sort, and so on.
284However, in the following discussion, we'll use the term method exclusively to
285mean methods of class instance objects, unless explicitly stated otherwise.)
286
287.. index:: object: method
288
289Valid method names of an instance object depend on its class. By definition,
290all attributes of a class that are function objects define corresponding
291methods of its instances. So in our example, ``x.f`` is a valid method
292reference, since ``MyClass.f`` is a function, but ``x.i`` is not, since
293``MyClass.i`` is not. But ``x.f`` is not the same thing as ``MyClass.f`` --- it
294is a *method object*, not a function object.
295
296
297.. _tut-methodobjects:
298
299Method Objects
300--------------
301
302Usually, a method is called right after it is bound::
303
304 x.f()
305
306In the :class:`MyClass` example, this will return the string ``'hello world'``.
307However, it is not necessary to call a method right away: ``x.f`` is a method
308object, and can be stored away and called at a later time. For example::
309
310 xf = x.f
311 while True:
312 print xf()
313
314will continue to print ``hello world`` until the end of time.
315
316What exactly happens when a method is called? You may have noticed that
317``x.f()`` was called without an argument above, even though the function
318definition for :meth:`f` specified an argument. What happened to the argument?
319Surely Python raises an exception when a function that requires an argument is
320called without any --- even if the argument isn't actually used...
321
322Actually, you may have guessed the answer: the special thing about methods is
323that the object is passed as the first argument of the function. In our
324example, the call ``x.f()`` is exactly equivalent to ``MyClass.f(x)``. In
325general, calling a method with a list of *n* arguments is equivalent to calling
326the corresponding function with an argument list that is created by inserting
327the method's object before the first argument.
328
329If you still don't understand how methods work, a look at the implementation can
330perhaps clarify matters. When an instance attribute is referenced that isn't a
331data attribute, its class is searched. If the name denotes a valid class
332attribute that is a function object, a method object is created by packing
333(pointers to) the instance object and the function object just found together in
334an abstract object: this is the method object. When the method object is called
Georg Brandla88fd762009-09-01 07:46:26 +0000335with an argument list, a new argument list is constructed from the instance
336object and the argument list, and the function object is called with this new
337argument list.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338
339
340.. _tut-remarks:
341
342Random Remarks
343==============
344
Georg Brandlb19be572007-12-29 10:57:00 +0000345.. These should perhaps be placed more carefully...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000346
347Data attributes override method attributes with the same name; to avoid
348accidental name conflicts, which may cause hard-to-find bugs in large programs,
349it is wise to use some kind of convention that minimizes the chance of
350conflicts. Possible conventions include capitalizing method names, prefixing
351data attribute names with a small unique string (perhaps just an underscore), or
352using verbs for methods and nouns for data attributes.
353
354Data attributes may be referenced by methods as well as by ordinary users
355("clients") of an object. In other words, classes are not usable to implement
356pure abstract data types. In fact, nothing in Python makes it possible to
357enforce data hiding --- it is all based upon convention. (On the other hand,
358the Python implementation, written in C, can completely hide implementation
359details and control access to an object if necessary; this can be used by
360extensions to Python written in C.)
361
362Clients should use data attributes with care --- clients may mess up invariants
363maintained by the methods by stamping on their data attributes. Note that
364clients may add data attributes of their own to an instance object without
365affecting the validity of the methods, as long as name conflicts are avoided ---
366again, a naming convention can save a lot of headaches here.
367
368There is no shorthand for referencing data attributes (or other methods!) from
369within methods. I find that this actually increases the readability of methods:
370there is no chance of confusing local variables and instance variables when
371glancing through a method.
372
373Often, the first argument of a method is called ``self``. This is nothing more
374than a convention: the name ``self`` has absolutely no special meaning to
Georg Brandl4938fef2009-07-29 17:50:25 +0000375Python. Note, however, that by not following the convention your code may be
Georg Brandl8ec7f652007-08-15 14:28:01 +0000376less readable to other Python programmers, and it is also conceivable that a
Georg Brandl4938fef2009-07-29 17:50:25 +0000377*class browser* program might be written that relies upon such a convention.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000378
379Any function object that is a class attribute defines a method for instances of
380that class. It is not necessary that the function definition is textually
381enclosed in the class definition: assigning a function object to a local
382variable in the class is also ok. For example::
383
384 # Function defined outside the class
385 def f1(self, x, y):
386 return min(x, x+y)
387
388 class C:
389 f = f1
390 def g(self):
391 return 'hello world'
392 h = g
393
394Now ``f``, ``g`` and ``h`` are all attributes of class :class:`C` that refer to
395function objects, and consequently they are all methods of instances of
396:class:`C` --- ``h`` being exactly equivalent to ``g``. Note that this practice
397usually only serves to confuse the reader of a program.
398
399Methods may call other methods by using method attributes of the ``self``
400argument::
401
402 class Bag:
403 def __init__(self):
404 self.data = []
405 def add(self, x):
406 self.data.append(x)
407 def addtwice(self, x):
408 self.add(x)
409 self.add(x)
410
411Methods may reference global names in the same way as ordinary functions. The
Terry Jan Reedy477a06c2012-01-11 14:51:07 -0500412global scope associated with a method is the module containing its
413definition. (A class is never used as a global scope.) While one
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414rarely encounters a good reason for using global data in a method, there are
415many legitimate uses of the global scope: for one thing, functions and modules
416imported into the global scope can be used by methods, as well as functions and
417classes defined in it. Usually, the class containing the method is itself
418defined in this global scope, and in the next section we'll find some good
Georg Brandl4938fef2009-07-29 17:50:25 +0000419reasons why a method would want to reference its own class.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000420
Georg Brandl6c45dc12008-03-06 07:31:34 +0000421Each value is an object, and therefore has a *class* (also called its *type*).
422It is stored as ``object.__class__``.
423
Georg Brandl8ec7f652007-08-15 14:28:01 +0000424
425.. _tut-inheritance:
426
427Inheritance
428===========
429
430Of course, a language feature would not be worthy of the name "class" without
431supporting inheritance. The syntax for a derived class definition looks like
432this::
433
434 class DerivedClassName(BaseClassName):
435 <statement-1>
436 .
437 .
438 .
439 <statement-N>
440
441The name :class:`BaseClassName` must be defined in a scope containing the
442derived class definition. In place of a base class name, other arbitrary
443expressions are also allowed. This can be useful, for example, when the base
444class is defined in another module::
445
446 class DerivedClassName(modname.BaseClassName):
447
448Execution of a derived class definition proceeds the same as for a base class.
449When the class object is constructed, the base class is remembered. This is
450used for resolving attribute references: if a requested attribute is not found
451in the class, the search proceeds to look in the base class. This rule is
452applied recursively if the base class itself is derived from some other class.
453
454There's nothing special about instantiation of derived classes:
455``DerivedClassName()`` creates a new instance of the class. Method references
456are resolved as follows: the corresponding class attribute is searched,
457descending down the chain of base classes if necessary, and the method reference
458is valid if this yields a function object.
459
460Derived classes may override methods of their base classes. Because methods
461have no special privileges when calling other methods of the same object, a
462method of a base class that calls another method defined in the same base class
463may end up calling a method of a derived class that overrides it. (For C++
Georg Brandlb19be572007-12-29 10:57:00 +0000464programmers: all methods in Python are effectively ``virtual``.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000465
466An overriding method in a derived class may in fact want to extend rather than
467simply replace the base class method of the same name. There is a simple way to
468call the base class method directly: just call ``BaseClassName.methodname(self,
469arguments)``. This is occasionally useful to clients as well. (Note that this
Georg Brandl4938fef2009-07-29 17:50:25 +0000470only works if the base class is accessible as ``BaseClassName`` in the global
Georg Brandl8ec7f652007-08-15 14:28:01 +0000471scope.)
472
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000473Python has two built-in functions that work with inheritance:
Georg Brandl6c45dc12008-03-06 07:31:34 +0000474
Georg Brandl4938fef2009-07-29 17:50:25 +0000475* Use :func:`isinstance` to check an instance's type: ``isinstance(obj, int)``
Georg Brandl6c45dc12008-03-06 07:31:34 +0000476 will be ``True`` only if ``obj.__class__`` is :class:`int` or some class
477 derived from :class:`int`.
478
479* Use :func:`issubclass` to check class inheritance: ``issubclass(bool, int)``
480 is ``True`` since :class:`bool` is a subclass of :class:`int`. However,
481 ``issubclass(unicode, str)`` is ``False`` since :class:`unicode` is not a
482 subclass of :class:`str` (they only share a common ancestor,
483 :class:`basestring`).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000484
Georg Brandl6c45dc12008-03-06 07:31:34 +0000485
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486
487.. _tut-multiple:
488
489Multiple Inheritance
490--------------------
491
492Python supports a limited form of multiple inheritance as well. A class
493definition with multiple base classes looks like this::
494
495 class DerivedClassName(Base1, Base2, Base3):
496 <statement-1>
497 .
498 .
499 .
500 <statement-N>
501
502For old-style classes, the only rule is depth-first, left-to-right. Thus, if an
503attribute is not found in :class:`DerivedClassName`, it is searched in
504:class:`Base1`, then (recursively) in the base classes of :class:`Base1`, and
505only if it is not found there, it is searched in :class:`Base2`, and so on.
506
507(To some people breadth first --- searching :class:`Base2` and :class:`Base3`
508before the base classes of :class:`Base1` --- looks more natural. However, this
509would require you to know whether a particular attribute of :class:`Base1` is
510actually defined in :class:`Base1` or in one of its base classes before you can
511figure out the consequences of a name conflict with an attribute of
512:class:`Base2`. The depth-first rule makes no differences between direct and
513inherited attributes of :class:`Base1`.)
514
Georg Brandla7395032007-10-21 12:15:05 +0000515For :term:`new-style class`\es, the method resolution order changes dynamically
516to support cooperative calls to :func:`super`. This approach is known in some
Georg Brandl8ec7f652007-08-15 14:28:01 +0000517other multiple-inheritance languages as call-next-method and is more powerful
518than the super call found in single-inheritance languages.
519
520With new-style classes, dynamic ordering is necessary because all cases of
Éric Araujo4fbd88a2011-03-22 21:45:42 +0100521multiple inheritance exhibit one or more diamond relationships (where at
Georg Brandl8ec7f652007-08-15 14:28:01 +0000522least one of the parent classes can be accessed through multiple paths from the
523bottommost class). For example, all new-style classes inherit from
524:class:`object`, so any case of multiple inheritance provides more than one path
525to reach :class:`object`. To keep the base classes from being accessed more
526than once, the dynamic algorithm linearizes the search order in a way that
527preserves the left-to-right ordering specified in each class, that calls each
528parent only once, and that is monotonic (meaning that a class can be subclassed
529without affecting the precedence order of its parents). Taken together, these
530properties make it possible to design reliable and extensible classes with
531multiple inheritance. For more detail, see
532http://www.python.org/download/releases/2.3/mro/.
533
534
535.. _tut-private:
536
Raymond Hettingerce0e0c72012-04-23 21:26:35 -0700537Private Variables and Class-local References
538============================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000539
Georg Brandl4938fef2009-07-29 17:50:25 +0000540"Private" instance variables that cannot be accessed except from inside an
Georg Brandlb20ada02010-06-12 06:26:54 +0000541object don't exist in Python. However, there is a convention that is followed
Georg Brandl4938fef2009-07-29 17:50:25 +0000542by most Python code: a name prefixed with an underscore (e.g. ``_spam``) should
543be treated as a non-public part of the API (whether it is a function, a method
544or a data member). It should be considered an implementation detail and subject
545to change without notice.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000546
Georg Brandl4938fef2009-07-29 17:50:25 +0000547Since there is a valid use-case for class-private members (namely to avoid name
548clashes of names with names defined by subclasses), there is limited support for
549such a mechanism, called :dfn:`name mangling`. Any identifier of the form
550``__spam`` (at least two leading underscores, at most one trailing underscore)
551is textually replaced with ``_classname__spam``, where ``classname`` is the
552current class name with leading underscore(s) stripped. This mangling is done
Georg Brandlaa66a962009-08-13 12:57:25 +0000553without regard to the syntactic position of the identifier, as long as it
554occurs within the definition of a class.
Georg Brandl4938fef2009-07-29 17:50:25 +0000555
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200556Name mangling is helpful for letting subclasses override methods without
557breaking intraclass method calls. For example::
558
Éric Araujo299172b2011-07-26 16:54:24 +0200559 class Mapping:
560 def __init__(self, iterable):
561 self.items_list = []
562 self.__update(iterable)
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200563
Éric Araujo299172b2011-07-26 16:54:24 +0200564 def update(self, iterable):
565 for item in iterable:
566 self.items_list.append(item)
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200567
Éric Araujo299172b2011-07-26 16:54:24 +0200568 __update = update # private copy of original update() method
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200569
Éric Araujo299172b2011-07-26 16:54:24 +0200570 class MappingSubclass(Mapping):
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200571
Éric Araujo299172b2011-07-26 16:54:24 +0200572 def update(self, keys, values):
573 # provides new signature for update()
574 # but does not break __init__()
575 for item in zip(keys, values):
576 self.items_list.append(item)
Raymond Hettingerfd1cb5962011-06-25 16:28:07 +0200577
Georg Brandl4938fef2009-07-29 17:50:25 +0000578Note that the mangling rules are designed mostly to avoid accidents; it still is
579possible to access or modify a variable that is considered private. This can
580even be useful in special circumstances, such as in the debugger.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000581
582Notice that code passed to ``exec``, ``eval()`` or ``execfile()`` does not
583consider the classname of the invoking class to be the current class; this is
584similar to the effect of the ``global`` statement, the effect of which is
585likewise restricted to code that is byte-compiled together. The same
586restriction applies to ``getattr()``, ``setattr()`` and ``delattr()``, as well
587as when referencing ``__dict__`` directly.
588
589
590.. _tut-odds:
591
592Odds and Ends
593=============
594
595Sometimes it is useful to have a data type similar to the Pascal "record" or C
596"struct", bundling together a few named data items. An empty class definition
597will do nicely::
598
599 class Employee:
600 pass
601
602 john = Employee() # Create an empty employee record
603
604 # Fill the fields of the record
605 john.name = 'John Doe'
606 john.dept = 'computer lab'
607 john.salary = 1000
608
609A piece of Python code that expects a particular abstract data type can often be
610passed a class that emulates the methods of that data type instead. For
611instance, if you have a function that formats some data from a file object, you
612can define a class with methods :meth:`read` and :meth:`readline` that get the
613data from a string buffer instead, and pass it as an argument.
614
Georg Brandlb19be572007-12-29 10:57:00 +0000615.. (Unfortunately, this technique has its limitations: a class can't define
616 operations that are accessed by special syntax such as sequence subscripting
617 or arithmetic operators, and assigning such a "pseudo-file" to sys.stdin will
618 not cause the interpreter to read further input from it.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619
620Instance method objects have attributes, too: ``m.im_self`` is the instance
621object with the method :meth:`m`, and ``m.im_func`` is the function object
622corresponding to the method.
623
624
625.. _tut-exceptionclasses:
626
627Exceptions Are Classes Too
628==========================
629
630User-defined exceptions are identified by classes as well. Using this mechanism
631it is possible to create extensible hierarchies of exceptions.
632
Georg Brandl4938fef2009-07-29 17:50:25 +0000633There are two new valid (semantic) forms for the :keyword:`raise` statement::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000634
635 raise Class, instance
636
637 raise instance
638
639In the first form, ``instance`` must be an instance of :class:`Class` or of a
640class derived from it. The second form is a shorthand for::
641
642 raise instance.__class__, instance
643
Georg Brandl4938fef2009-07-29 17:50:25 +0000644A class in an :keyword:`except` clause is compatible with an exception if it is
645the same class or a base class thereof (but not the other way around --- an
646except clause listing a derived class is not compatible with a base class). For
647example, the following code will print B, C, D in that order::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000648
649 class B:
650 pass
651 class C(B):
652 pass
653 class D(C):
654 pass
655
656 for c in [B, C, D]:
657 try:
658 raise c()
659 except D:
660 print "D"
661 except C:
662 print "C"
663 except B:
664 print "B"
665
666Note that if the except clauses were reversed (with ``except B`` first), it
667would have printed B, B, B --- the first matching except clause is triggered.
668
669When an error message is printed for an unhandled exception, the exception's
670class name is printed, then a colon and a space, and finally the instance
671converted to a string using the built-in function :func:`str`.
672
673
674.. _tut-iterators:
675
676Iterators
677=========
678
679By now you have probably noticed that most container objects can be looped over
680using a :keyword:`for` statement::
681
682 for element in [1, 2, 3]:
683 print element
684 for element in (1, 2, 3):
685 print element
686 for key in {'one':1, 'two':2}:
687 print key
688 for char in "123":
689 print char
690 for line in open("myfile.txt"):
Andrew Svetlovbd5279e2012-12-08 18:01:27 +0200691 print line,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000692
693This style of access is clear, concise, and convenient. The use of iterators
694pervades and unifies Python. Behind the scenes, the :keyword:`for` statement
695calls :func:`iter` on the container object. The function returns an iterator
696object that defines the method :meth:`next` which accesses elements in the
697container one at a time. When there are no more elements, :meth:`next` raises a
698:exc:`StopIteration` exception which tells the :keyword:`for` loop to terminate.
699This example shows how it all works::
700
701 >>> s = 'abc'
702 >>> it = iter(s)
703 >>> it
704 <iterator object at 0x00A1DB50>
705 >>> it.next()
706 'a'
707 >>> it.next()
708 'b'
709 >>> it.next()
710 'c'
711 >>> it.next()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000712 Traceback (most recent call last):
713 File "<stdin>", line 1, in ?
714 it.next()
715 StopIteration
716
717Having seen the mechanics behind the iterator protocol, it is easy to add
Georg Brandl2c108402010-08-01 21:28:47 +0000718iterator behavior to your classes. Define an :meth:`__iter__` method which
Georg Brandl8ec7f652007-08-15 14:28:01 +0000719returns an object with a :meth:`next` method. If the class defines
720:meth:`next`, then :meth:`__iter__` can just return ``self``::
721
722 class Reverse:
Georg Brandl12b8fcf2011-05-01 22:36:31 +0200723 """Iterator for looping over a sequence backwards."""
Georg Brandl8ec7f652007-08-15 14:28:01 +0000724 def __init__(self, data):
725 self.data = data
726 self.index = len(data)
727 def __iter__(self):
728 return self
729 def next(self):
730 if self.index == 0:
731 raise StopIteration
732 self.index = self.index - 1
733 return self.data[self.index]
734
Georg Brandl12b8fcf2011-05-01 22:36:31 +0200735::
736
Georg Brandl2c108402010-08-01 21:28:47 +0000737 >>> rev = Reverse('spam')
738 >>> iter(rev)
739 <__main__.Reverse object at 0x00A1DB50>
740 >>> for char in rev:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000741 ... print char
742 ...
743 m
744 a
745 p
746 s
747
748
749.. _tut-generators:
750
751Generators
752==========
753
Georg Brandlcf3fb252007-10-21 10:52:38 +0000754:term:`Generator`\s are a simple and powerful tool for creating iterators. They
755are written like regular functions but use the :keyword:`yield` statement
756whenever they want to return data. Each time :meth:`next` is called, the
757generator resumes where it left-off (it remembers all the data values and which
758statement was last executed). An example shows that generators can be trivially
759easy to create::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000760
761 def reverse(data):
762 for index in range(len(data)-1, -1, -1):
763 yield data[index]
764
Georg Brandl12b8fcf2011-05-01 22:36:31 +0200765::
766
Georg Brandl8ec7f652007-08-15 14:28:01 +0000767 >>> for char in reverse('golf'):
768 ... print char
769 ...
770 f
771 l
772 o
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000773 g
Georg Brandl8ec7f652007-08-15 14:28:01 +0000774
775Anything that can be done with generators can also be done with class based
776iterators as described in the previous section. What makes generators so
777compact is that the :meth:`__iter__` and :meth:`next` methods are created
778automatically.
779
780Another key feature is that the local variables and execution state are
781automatically saved between calls. This made the function easier to write and
782much more clear than an approach using instance variables like ``self.index``
783and ``self.data``.
784
785In addition to automatic method creation and saving program state, when
786generators terminate, they automatically raise :exc:`StopIteration`. In
787combination, these features make it easy to create iterators with no more effort
788than writing a regular function.
789
790
791.. _tut-genexps:
792
793Generator Expressions
794=====================
795
796Some simple generators can be coded succinctly as expressions using a syntax
797similar to list comprehensions but with parentheses instead of brackets. These
798expressions are designed for situations where the generator is used right away
799by an enclosing function. Generator expressions are more compact but less
800versatile than full generator definitions and tend to be more memory friendly
801than equivalent list comprehensions.
802
803Examples::
804
805 >>> sum(i*i for i in range(10)) # sum of squares
806 285
807
808 >>> xvec = [10, 20, 30]
809 >>> yvec = [7, 5, 3]
810 >>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
811 260
812
813 >>> from math import pi, sin
814 >>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91))
815
816 >>> unique_words = set(word for line in page for word in line.split())
817
818 >>> valedictorian = max((student.gpa, student.name) for student in graduates)
819
820 >>> data = 'golf'
821 >>> list(data[i] for i in range(len(data)-1,-1,-1))
822 ['f', 'l', 'o', 'g']
823
824
825
826.. rubric:: Footnotes
827
828.. [#] Except for one thing. Module objects have a secret read-only attribute called
829 :attr:`__dict__` which returns the dictionary used to implement the module's
830 namespace; the name :attr:`__dict__` is an attribute but not a global name.
831 Obviously, using this violates the abstraction of namespace implementation, and
832 should be restricted to things like post-mortem debuggers.
833