Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _tut-classes: |
| 2 | |
| 3 | ******* |
| 4 | Classes |
| 5 | ******* |
| 6 | |
Georg Brandl | a192828 | 2010-10-17 10:44:11 +0000 | [diff] [blame] | 7 | Compared with other programming languages, Python's class mechanism adds classes |
| 8 | with a minimum of new syntax and semantics. It is a mixture of the class |
| 9 | mechanisms found in C++ and Modula-3. Python classes provide all the standard |
| 10 | features of Object Oriented Programming: the class inheritance mechanism allows |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | multiple base classes, a derived class can override any methods of its base |
| 12 | class or classes, and a method can call the method of a base class with the same |
Georg Brandl | a192828 | 2010-10-17 10:44:11 +0000 | [diff] [blame] | 13 | name. Objects can contain arbitrary amounts and kinds of data. As is true for |
| 14 | modules, classes partake of the dynamic nature of Python: they are created at |
| 15 | runtime, and can be modified further after creation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 17 | In C++ terminology, normally class members (including the data members) are |
Georg Brandl | a192828 | 2010-10-17 10:44:11 +0000 | [diff] [blame] | 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 |
| 20 | members from its methods: the method function is declared with an explicit first |
| 21 | argument representing the object, which is provided implicitly by the call. As |
| 22 | in Smalltalk, classes themselves are objects. This provides semantics for |
| 23 | importing and renaming. Unlike C++ and Modula-3, built-in types can be used as |
| 24 | base classes for extension by the user. Also, like in C++, most built-in |
| 25 | operators with special syntax (arithmetic operators, subscripting etc.) can be |
| 26 | redefined for class instances. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 28 | (Lacking universally accepted terminology to talk about classes, I will make |
| 29 | occasional use of Smalltalk and C++ terms. I would use Modula-3 terms, since |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | its object-oriented semantics are closer to those of Python than C++, but I |
| 31 | expect that few readers have heard of it.) |
| 32 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 33 | |
| 34 | .. _tut-object: |
| 35 | |
| 36 | A Word About Names and Objects |
| 37 | ============================== |
| 38 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | Objects have individuality, and multiple names (in multiple scopes) can be bound |
| 40 | to the same object. This is known as aliasing in other languages. This is |
| 41 | usually not appreciated on a first glance at Python, and can be safely ignored |
| 42 | when dealing with immutable basic types (numbers, strings, tuples). However, |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 43 | aliasing has a possibly surprising effect on the semantics of Python code |
| 44 | involving mutable objects such as lists, dictionaries, and most other types. |
| 45 | This is usually used to the benefit of the program, since aliases behave like |
| 46 | pointers in some respects. For example, passing an object is cheap since only a |
| 47 | pointer is passed by the implementation; and if a function modifies an object |
| 48 | passed as an argument, the caller will see the change --- this eliminates the |
| 49 | need for two different argument passing mechanisms as in Pascal. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | .. _tut-scopes: |
| 53 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 54 | Python Scopes and Namespaces |
| 55 | ============================ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 57 | Before introducing classes, I first have to tell you something about Python's |
| 58 | scope rules. Class definitions play some neat tricks with namespaces, and you |
| 59 | need to know how scopes and namespaces work to fully understand what's going on. |
| 60 | Incidentally, knowledge about this subject is useful for any advanced Python |
| 61 | programmer. |
| 62 | |
| 63 | Let's begin with some definitions. |
| 64 | |
| 65 | A *namespace* is a mapping from names to objects. Most namespaces are currently |
| 66 | implemented as Python dictionaries, but that's normally not noticeable in any |
| 67 | way (except for performance), and it may change in the future. Examples of |
Georg Brandl | 17dafdc | 2010-08-02 20:44:34 +0000 | [diff] [blame] | 68 | namespaces are: the set of built-in names (containing functions such as :func:`abs`, and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | built-in exception names); the global names in a module; and the local names in |
| 70 | a function invocation. In a sense the set of attributes of an object also form |
| 71 | a namespace. The important thing to know about namespaces is that there is |
| 72 | absolutely no relation between names in different namespaces; for instance, two |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 73 | different modules may both define a function ``maximize`` without confusion --- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | users of the modules must prefix it with the module name. |
| 75 | |
| 76 | By the way, I use the word *attribute* for any name following a dot --- for |
| 77 | example, in the expression ``z.real``, ``real`` is an attribute of the object |
| 78 | ``z``. Strictly speaking, references to names in modules are attribute |
| 79 | references: in the expression ``modname.funcname``, ``modname`` is a module |
| 80 | object and ``funcname`` is an attribute of it. In this case there happens to be |
| 81 | a straightforward mapping between the module's attributes and the global names |
| 82 | defined in the module: they share the same namespace! [#]_ |
| 83 | |
| 84 | Attributes may be read-only or writable. In the latter case, assignment to |
| 85 | attributes 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 |
| 88 | the attribute :attr:`the_answer` from the object named by ``modname``. |
| 89 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 90 | Namespaces are created at different moments and have different lifetimes. The |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | namespace containing the built-in names is created when the Python interpreter |
| 92 | starts up, and is never deleted. The global namespace for a module is created |
| 93 | when the module definition is read in; normally, module namespaces also last |
| 94 | until the interpreter quits. The statements executed by the top-level |
| 95 | invocation of the interpreter, either read from a script file or interactively, |
| 96 | are considered part of a module called :mod:`__main__`, so they have their own |
| 97 | global namespace. (The built-in names actually also live in a module; this is |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 98 | called :mod:`builtins`.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
| 100 | The local namespace for a function is created when the function is called, and |
| 101 | deleted when the function returns or raises an exception that is not handled |
| 102 | within the function. (Actually, forgetting would be a better way to describe |
| 103 | what actually happens.) Of course, recursive invocations each have their own |
| 104 | local namespace. |
| 105 | |
| 106 | A *scope* is a textual region of a Python program where a namespace is directly |
| 107 | accessible. "Directly accessible" here means that an unqualified reference to a |
| 108 | name attempts to find the name in the namespace. |
| 109 | |
| 110 | Although scopes are determined statically, they are used dynamically. At any |
| 111 | time during execution, there are at least three nested scopes whose namespaces |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 112 | are 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
| 120 | If a name is declared global, then all references and assignments go directly to |
Georg Brandl | fed7d80 | 2008-12-05 18:06:58 +0000 | [diff] [blame] | 121 | the middle scope containing the module's global names. To rebind variables |
| 122 | found outside of the innermost scope, the :keyword:`nonlocal` statement can be |
| 123 | used; if not declared nonlocal, those variable are read-only (an attempt to |
| 124 | write to such a variable will simply create a *new* local variable in the |
| 125 | innermost scope, leaving the identically named outer variable unchanged). |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | Usually, the local scope references the local names of the (textually) current |
| 128 | function. Outside functions, the local scope references the same namespace as |
| 129 | the global scope: the module's namespace. Class definitions place yet another |
| 130 | namespace in the local scope. |
| 131 | |
| 132 | It is important to realize that scopes are determined textually: the global |
| 133 | scope of a function defined in a module is that module's namespace, no matter |
| 134 | from where or by what alias the function is called. On the other hand, the |
| 135 | actual search for names is done dynamically, at run time --- however, the |
| 136 | language definition is evolving towards static name resolution, at "compile" |
| 137 | time, so don't rely on dynamic name resolution! (In fact, local variables are |
| 138 | already determined statically.) |
| 139 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 140 | A special quirk of Python is that -- if no :keyword:`global` statement is in |
| 141 | effect -- assignments to names always go into the innermost scope. Assignments |
| 142 | do not copy data --- they just bind names to objects. The same is true for |
| 143 | deletions: the statement ``del x`` removes the binding of ``x`` from the |
| 144 | namespace referenced by the local scope. In fact, all operations that introduce |
| 145 | new names use the local scope: in particular, :keyword:`import` statements and |
Georg Brandl | 3517e37 | 2009-08-13 11:55:03 +0000 | [diff] [blame] | 146 | function definitions bind the module or function name in the local scope. |
Georg Brandl | c5d98b4 | 2007-12-04 18:11:03 +0000 | [diff] [blame] | 147 | |
| 148 | The :keyword:`global` statement can be used to indicate that particular |
| 149 | variables live in the global scope and should be rebound there; the |
| 150 | :keyword:`nonlocal` statement indicates that particular variables live in |
| 151 | an enclosing scope and should be rebound there. |
| 152 | |
| 153 | .. _tut-scopeexample: |
| 154 | |
| 155 | Scopes and Namespaces Example |
| 156 | ----------------------------- |
| 157 | |
| 158 | This is an example demonstrating how to reference the different scopes and |
| 159 | namespaces, and how :keyword:`global` and :keyword:`nonlocal` affect variable |
| 160 | binding:: |
| 161 | |
| 162 | def scope_test(): |
| 163 | def do_local(): |
| 164 | spam = "local spam" |
| 165 | def do_nonlocal(): |
| 166 | nonlocal spam |
| 167 | spam = "nonlocal spam" |
| 168 | def do_global(): |
| 169 | global spam |
| 170 | spam = "global spam" |
Georg Brandl | c5d98b4 | 2007-12-04 18:11:03 +0000 | [diff] [blame] | 171 | spam = "test spam" |
| 172 | do_local() |
| 173 | print("After local assignment:", spam) |
| 174 | do_nonlocal() |
| 175 | print("After nonlocal assignment:", spam) |
| 176 | do_global() |
| 177 | print("After global assignment:", spam) |
| 178 | |
| 179 | scope_test() |
| 180 | print("In global scope:", spam) |
| 181 | |
Senthil Kumaran | 74d5657 | 2012-03-08 20:54:34 -0800 | [diff] [blame] | 182 | The output of the example code is: |
| 183 | |
| 184 | .. code-block:: none |
Georg Brandl | c5d98b4 | 2007-12-04 18:11:03 +0000 | [diff] [blame] | 185 | |
| 186 | After local assignment: test spam |
| 187 | After nonlocal assignment: nonlocal spam |
| 188 | After global assignment: nonlocal spam |
| 189 | In global scope: global spam |
| 190 | |
| 191 | Note how the *local* assignment (which is default) didn't change *scope_test*\'s |
| 192 | binding of *spam*. The :keyword:`nonlocal` assignment changed *scope_test*\'s |
| 193 | binding of *spam*, and the :keyword:`global` assignment changed the module-level |
| 194 | binding. |
| 195 | |
| 196 | You can also see that there was no previous binding for *spam* before the |
| 197 | :keyword:`global` assignment. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | .. _tut-firstclasses: |
| 201 | |
| 202 | A First Look at Classes |
| 203 | ======================= |
| 204 | |
| 205 | Classes introduce a little bit of new syntax, three new object types, and some |
| 206 | new semantics. |
| 207 | |
| 208 | |
| 209 | .. _tut-classdefinition: |
| 210 | |
| 211 | Class Definition Syntax |
| 212 | ----------------------- |
| 213 | |
| 214 | The simplest form of class definition looks like this:: |
| 215 | |
| 216 | class ClassName: |
| 217 | <statement-1> |
| 218 | . |
| 219 | . |
| 220 | . |
| 221 | <statement-N> |
| 222 | |
| 223 | Class definitions, like function definitions (:keyword:`def` statements) must be |
| 224 | executed before they have any effect. (You could conceivably place a class |
| 225 | definition in a branch of an :keyword:`if` statement, or inside a function.) |
| 226 | |
| 227 | In practice, the statements inside a class definition will usually be function |
| 228 | definitions, but other statements are allowed, and sometimes useful --- we'll |
| 229 | come back to this later. The function definitions inside a class normally have |
| 230 | a peculiar form of argument list, dictated by the calling conventions for |
| 231 | methods --- again, this is explained later. |
| 232 | |
| 233 | When a class definition is entered, a new namespace is created, and used as the |
| 234 | local scope --- thus, all assignments to local variables go into this new |
| 235 | namespace. In particular, function definitions bind the name of the new |
| 236 | function here. |
| 237 | |
| 238 | When a class definition is left normally (via the end), a *class object* is |
| 239 | created. This is basically a wrapper around the contents of the namespace |
| 240 | created by the class definition; we'll learn more about class objects in the |
| 241 | next section. The original local scope (the one in effect just before the class |
| 242 | definition was entered) is reinstated, and the class object is bound here to the |
| 243 | class name given in the class definition header (:class:`ClassName` in the |
| 244 | example). |
| 245 | |
| 246 | |
| 247 | .. _tut-classobjects: |
| 248 | |
| 249 | Class Objects |
| 250 | ------------- |
| 251 | |
| 252 | Class objects support two kinds of operations: attribute references and |
| 253 | instantiation. |
| 254 | |
| 255 | *Attribute references* use the standard syntax used for all attribute references |
| 256 | in Python: ``obj.name``. Valid attribute names are all the names that were in |
| 257 | the class's namespace when the class object was created. So, if the class |
| 258 | definition looked like this:: |
| 259 | |
| 260 | class MyClass: |
Georg Brandl | 5d955ed | 2008-09-13 17:18:21 +0000 | [diff] [blame] | 261 | """A simple example class""" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | i = 12345 |
| 263 | def f(self): |
| 264 | return 'hello world' |
| 265 | |
| 266 | then ``MyClass.i`` and ``MyClass.f`` are valid attribute references, returning |
| 267 | an integer and a function object, respectively. Class attributes can also be |
| 268 | assigned to, so you can change the value of ``MyClass.i`` by assignment. |
| 269 | :attr:`__doc__` is also a valid attribute, returning the docstring belonging to |
| 270 | the class: ``"A simple example class"``. |
| 271 | |
| 272 | Class *instantiation* uses function notation. Just pretend that the class |
| 273 | object is a parameterless function that returns a new instance of the class. |
| 274 | For example (assuming the above class):: |
| 275 | |
| 276 | x = MyClass() |
| 277 | |
| 278 | creates a new *instance* of the class and assigns this object to the local |
| 279 | variable ``x``. |
| 280 | |
| 281 | The instantiation operation ("calling" a class object) creates an empty object. |
| 282 | Many classes like to create objects with instances customized to a specific |
| 283 | initial state. Therefore a class may define a special method named |
| 284 | :meth:`__init__`, like this:: |
| 285 | |
| 286 | def __init__(self): |
| 287 | self.data = [] |
| 288 | |
| 289 | When a class defines an :meth:`__init__` method, class instantiation |
| 290 | automatically invokes :meth:`__init__` for the newly-created class instance. So |
| 291 | in this example, a new, initialized instance can be obtained by:: |
| 292 | |
| 293 | x = MyClass() |
| 294 | |
| 295 | Of course, the :meth:`__init__` method may have arguments for greater |
| 296 | flexibility. In that case, arguments given to the class instantiation operator |
| 297 | are passed on to :meth:`__init__`. For example, :: |
| 298 | |
| 299 | >>> class Complex: |
| 300 | ... def __init__(self, realpart, imagpart): |
| 301 | ... self.r = realpart |
| 302 | ... self.i = imagpart |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 303 | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | >>> x = Complex(3.0, -4.5) |
| 305 | >>> x.r, x.i |
| 306 | (3.0, -4.5) |
| 307 | |
| 308 | |
| 309 | .. _tut-instanceobjects: |
| 310 | |
| 311 | Instance Objects |
| 312 | ---------------- |
| 313 | |
| 314 | Now what can we do with instance objects? The only operations understood by |
| 315 | instance objects are attribute references. There are two kinds of valid |
| 316 | attribute names, data attributes and methods. |
| 317 | |
| 318 | *data attributes* correspond to "instance variables" in Smalltalk, and to "data |
| 319 | members" in C++. Data attributes need not be declared; like local variables, |
| 320 | they spring into existence when they are first assigned to. For example, if |
| 321 | ``x`` is the instance of :class:`MyClass` created above, the following piece of |
| 322 | code will print the value ``16``, without leaving a trace:: |
| 323 | |
| 324 | x.counter = 1 |
| 325 | while x.counter < 10: |
| 326 | x.counter = x.counter * 2 |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 327 | print(x.counter) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 328 | del x.counter |
| 329 | |
| 330 | The other kind of instance attribute reference is a *method*. A method is a |
| 331 | function that "belongs to" an object. (In Python, the term method is not unique |
| 332 | to class instances: other object types can have methods as well. For example, |
| 333 | list objects have methods called append, insert, remove, sort, and so on. |
| 334 | However, in the following discussion, we'll use the term method exclusively to |
| 335 | mean methods of class instance objects, unless explicitly stated otherwise.) |
| 336 | |
| 337 | .. index:: object: method |
| 338 | |
| 339 | Valid method names of an instance object depend on its class. By definition, |
| 340 | all attributes of a class that are function objects define corresponding |
| 341 | methods of its instances. So in our example, ``x.f`` is a valid method |
| 342 | reference, since ``MyClass.f`` is a function, but ``x.i`` is not, since |
| 343 | ``MyClass.i`` is not. But ``x.f`` is not the same thing as ``MyClass.f`` --- it |
| 344 | is a *method object*, not a function object. |
| 345 | |
| 346 | |
| 347 | .. _tut-methodobjects: |
| 348 | |
| 349 | Method Objects |
| 350 | -------------- |
| 351 | |
| 352 | Usually, a method is called right after it is bound:: |
| 353 | |
| 354 | x.f() |
| 355 | |
| 356 | In the :class:`MyClass` example, this will return the string ``'hello world'``. |
| 357 | However, it is not necessary to call a method right away: ``x.f`` is a method |
| 358 | object, and can be stored away and called at a later time. For example:: |
| 359 | |
| 360 | xf = x.f |
| 361 | while True: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 362 | print(xf()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
| 364 | will continue to print ``hello world`` until the end of time. |
| 365 | |
| 366 | What exactly happens when a method is called? You may have noticed that |
| 367 | ``x.f()`` was called without an argument above, even though the function |
| 368 | definition for :meth:`f` specified an argument. What happened to the argument? |
| 369 | Surely Python raises an exception when a function that requires an argument is |
| 370 | called without any --- even if the argument isn't actually used... |
| 371 | |
| 372 | Actually, you may have guessed the answer: the special thing about methods is |
| 373 | that the object is passed as the first argument of the function. In our |
| 374 | example, the call ``x.f()`` is exactly equivalent to ``MyClass.f(x)``. In |
| 375 | general, calling a method with a list of *n* arguments is equivalent to calling |
| 376 | the corresponding function with an argument list that is created by inserting |
| 377 | the method's object before the first argument. |
| 378 | |
| 379 | If you still don't understand how methods work, a look at the implementation can |
| 380 | perhaps clarify matters. When an instance attribute is referenced that isn't a |
| 381 | data attribute, its class is searched. If the name denotes a valid class |
| 382 | attribute that is a function object, a method object is created by packing |
| 383 | (pointers to) the instance object and the function object just found together in |
| 384 | an abstract object: this is the method object. When the method object is called |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 385 | with an argument list, a new argument list is constructed from the instance |
| 386 | object and the argument list, and the function object is called with this new |
| 387 | argument list. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 388 | |
| 389 | |
Raymond Hettinger | 04ba0bb | 2014-06-23 18:08:01 -0700 | [diff] [blame] | 390 | .. _tut-class-and-instance-variables: |
| 391 | |
| 392 | Class and Instance Variables |
| 393 | ---------------------------- |
| 394 | |
| 395 | Generally speaking, instance variables are for data unique to each instance |
| 396 | and class variables are for attributes and methods shared by all instances |
| 397 | of the class:: |
| 398 | |
| 399 | class Dog: |
| 400 | |
| 401 | kind = 'canine' # class variable shared by all instances |
| 402 | |
| 403 | def __init__(self, name): |
| 404 | self.name = name # instance variable unique to each instance |
| 405 | |
| 406 | >>> d = Dog('Fido') |
| 407 | >>> e = Dog('Buddy') |
| 408 | >>> d.kind # shared by all dogs |
| 409 | 'canine' |
| 410 | >>> e.kind # shared by all dogs |
| 411 | 'canine' |
| 412 | >>> d.name # unique to d |
| 413 | 'Fido' |
| 414 | >>> e.name # unique to e |
| 415 | 'Buddy' |
| 416 | |
| 417 | As discussed in :ref:`tut-object`, shared data can have possibly surprising |
| 418 | effects with involving :term:`mutable` objects such as lists and dictionaries. |
| 419 | For example, the *tricks* list in the following code should not be used as a |
| 420 | class variable because just a single list would be shared by all *Dog* |
| 421 | instances:: |
| 422 | |
| 423 | class Dog: |
| 424 | |
| 425 | tricks = [] # mistaken use of a class variable |
| 426 | |
| 427 | def __init__(self, name): |
| 428 | self.name = name |
| 429 | |
| 430 | def add_trick(self, trick): |
| 431 | self.tricks.append(trick) |
| 432 | |
| 433 | >>> d = Dog('Fido') |
| 434 | >>> e = Dog('Buddy') |
| 435 | >>> d.add_trick('roll over') |
| 436 | >>> e.add_trick('play dead') |
| 437 | >>> d.tricks # unexpectedly shared by all dogs |
| 438 | ['roll over', 'play dead'] |
| 439 | |
| 440 | Correct design of the class should use an instance variable instead:: |
| 441 | |
| 442 | class Dog: |
| 443 | |
| 444 | def __init__(self, name): |
| 445 | self.name = name |
| 446 | self.tricks = [] # creates a new empty list for each dog |
| 447 | |
| 448 | def add_trick(self, trick): |
| 449 | self.tricks.append(trick) |
| 450 | |
| 451 | >>> d = Dog('Fido') |
| 452 | >>> e = Dog('Buddy') |
| 453 | >>> d.add_trick('roll over') |
| 454 | >>> e.add_trick('play dead') |
| 455 | >>> d.tricks |
| 456 | ['roll over'] |
| 457 | >>> e.tricks |
| 458 | ['play dead'] |
| 459 | |
| 460 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 461 | .. _tut-remarks: |
| 462 | |
| 463 | Random Remarks |
| 464 | ============== |
| 465 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 466 | .. These should perhaps be placed more carefully... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | |
| 468 | Data attributes override method attributes with the same name; to avoid |
| 469 | accidental name conflicts, which may cause hard-to-find bugs in large programs, |
| 470 | it is wise to use some kind of convention that minimizes the chance of |
| 471 | conflicts. Possible conventions include capitalizing method names, prefixing |
| 472 | data attribute names with a small unique string (perhaps just an underscore), or |
| 473 | using verbs for methods and nouns for data attributes. |
| 474 | |
| 475 | Data attributes may be referenced by methods as well as by ordinary users |
| 476 | ("clients") of an object. In other words, classes are not usable to implement |
| 477 | pure abstract data types. In fact, nothing in Python makes it possible to |
| 478 | enforce data hiding --- it is all based upon convention. (On the other hand, |
| 479 | the Python implementation, written in C, can completely hide implementation |
| 480 | details and control access to an object if necessary; this can be used by |
| 481 | extensions to Python written in C.) |
| 482 | |
| 483 | Clients should use data attributes with care --- clients may mess up invariants |
| 484 | maintained by the methods by stamping on their data attributes. Note that |
| 485 | clients may add data attributes of their own to an instance object without |
| 486 | affecting the validity of the methods, as long as name conflicts are avoided --- |
| 487 | again, a naming convention can save a lot of headaches here. |
| 488 | |
| 489 | There is no shorthand for referencing data attributes (or other methods!) from |
| 490 | within methods. I find that this actually increases the readability of methods: |
| 491 | there is no chance of confusing local variables and instance variables when |
| 492 | glancing through a method. |
| 493 | |
| 494 | Often, the first argument of a method is called ``self``. This is nothing more |
| 495 | than a convention: the name ``self`` has absolutely no special meaning to |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 496 | Python. Note, however, that by not following the convention your code may be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 497 | less readable to other Python programmers, and it is also conceivable that a |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 498 | *class browser* program might be written that relies upon such a convention. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | |
| 500 | Any function object that is a class attribute defines a method for instances of |
| 501 | that class. It is not necessary that the function definition is textually |
| 502 | enclosed in the class definition: assigning a function object to a local |
| 503 | variable in the class is also ok. For example:: |
| 504 | |
| 505 | # Function defined outside the class |
| 506 | def f1(self, x, y): |
| 507 | return min(x, x+y) |
| 508 | |
| 509 | class C: |
| 510 | f = f1 |
| 511 | def g(self): |
| 512 | return 'hello world' |
| 513 | h = g |
| 514 | |
| 515 | Now ``f``, ``g`` and ``h`` are all attributes of class :class:`C` that refer to |
| 516 | function objects, and consequently they are all methods of instances of |
| 517 | :class:`C` --- ``h`` being exactly equivalent to ``g``. Note that this practice |
| 518 | usually only serves to confuse the reader of a program. |
| 519 | |
| 520 | Methods may call other methods by using method attributes of the ``self`` |
| 521 | argument:: |
| 522 | |
| 523 | class Bag: |
| 524 | def __init__(self): |
| 525 | self.data = [] |
| 526 | def add(self, x): |
| 527 | self.data.append(x) |
| 528 | def addtwice(self, x): |
| 529 | self.add(x) |
| 530 | self.add(x) |
| 531 | |
| 532 | Methods may reference global names in the same way as ordinary functions. The |
Terry Jan Reedy | ea868d3 | 2012-01-11 14:54:34 -0500 | [diff] [blame] | 533 | global scope associated with a method is the module containing its |
| 534 | definition. (A class is never used as a global scope.) While one |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | rarely encounters a good reason for using global data in a method, there are |
| 536 | many legitimate uses of the global scope: for one thing, functions and modules |
| 537 | imported into the global scope can be used by methods, as well as functions and |
| 538 | classes defined in it. Usually, the class containing the method is itself |
| 539 | defined in this global scope, and in the next section we'll find some good |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 540 | reasons why a method would want to reference its own class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 541 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 542 | Each value is an object, and therefore has a *class* (also called its *type*). |
| 543 | It is stored as ``object.__class__``. |
| 544 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 545 | |
| 546 | .. _tut-inheritance: |
| 547 | |
| 548 | Inheritance |
| 549 | =========== |
| 550 | |
| 551 | Of course, a language feature would not be worthy of the name "class" without |
| 552 | supporting inheritance. The syntax for a derived class definition looks like |
| 553 | this:: |
| 554 | |
| 555 | class DerivedClassName(BaseClassName): |
| 556 | <statement-1> |
| 557 | . |
| 558 | . |
| 559 | . |
| 560 | <statement-N> |
| 561 | |
| 562 | The name :class:`BaseClassName` must be defined in a scope containing the |
| 563 | derived class definition. In place of a base class name, other arbitrary |
| 564 | expressions are also allowed. This can be useful, for example, when the base |
| 565 | class is defined in another module:: |
| 566 | |
| 567 | class DerivedClassName(modname.BaseClassName): |
| 568 | |
| 569 | Execution of a derived class definition proceeds the same as for a base class. |
| 570 | When the class object is constructed, the base class is remembered. This is |
| 571 | used for resolving attribute references: if a requested attribute is not found |
| 572 | in the class, the search proceeds to look in the base class. This rule is |
| 573 | applied recursively if the base class itself is derived from some other class. |
| 574 | |
| 575 | There's nothing special about instantiation of derived classes: |
| 576 | ``DerivedClassName()`` creates a new instance of the class. Method references |
| 577 | are resolved as follows: the corresponding class attribute is searched, |
| 578 | descending down the chain of base classes if necessary, and the method reference |
| 579 | is valid if this yields a function object. |
| 580 | |
| 581 | Derived classes may override methods of their base classes. Because methods |
| 582 | have no special privileges when calling other methods of the same object, a |
| 583 | method of a base class that calls another method defined in the same base class |
| 584 | may end up calling a method of a derived class that overrides it. (For C++ |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 585 | programmers: all methods in Python are effectively ``virtual``.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 586 | |
| 587 | An overriding method in a derived class may in fact want to extend rather than |
| 588 | simply replace the base class method of the same name. There is a simple way to |
| 589 | call the base class method directly: just call ``BaseClassName.methodname(self, |
| 590 | arguments)``. This is occasionally useful to clients as well. (Note that this |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 591 | only works if the base class is accessible as ``BaseClassName`` in the global |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 592 | scope.) |
| 593 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 594 | Python has two built-in functions that work with inheritance: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 595 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 596 | * Use :func:`isinstance` to check an instance's type: ``isinstance(obj, int)`` |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 597 | will be ``True`` only if ``obj.__class__`` is :class:`int` or some class |
| 598 | derived from :class:`int`. |
| 599 | |
| 600 | * Use :func:`issubclass` to check class inheritance: ``issubclass(bool, int)`` |
| 601 | is ``True`` since :class:`bool` is a subclass of :class:`int`. However, |
Georg Brandl | 01ca04c | 2008-07-16 21:21:29 +0000 | [diff] [blame] | 602 | ``issubclass(float, int)`` is ``False`` since :class:`float` is not a |
| 603 | subclass of :class:`int`. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 604 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 605 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 606 | |
| 607 | .. _tut-multiple: |
| 608 | |
| 609 | Multiple Inheritance |
| 610 | -------------------- |
| 611 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 612 | Python supports a form of multiple inheritance as well. A class definition with |
| 613 | multiple base classes looks like this:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 614 | |
| 615 | class DerivedClassName(Base1, Base2, Base3): |
| 616 | <statement-1> |
| 617 | . |
| 618 | . |
| 619 | . |
| 620 | <statement-N> |
| 621 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 622 | For most purposes, in the simplest cases, you can think of the search for |
| 623 | attributes inherited from a parent class as depth-first, left-to-right, not |
| 624 | searching twice in the same class where there is an overlap in the hierarchy. |
| 625 | Thus, if an attribute is not found in :class:`DerivedClassName`, it is searched |
| 626 | for in :class:`Base1`, then (recursively) in the base classes of :class:`Base1`, |
| 627 | and if it was not found there, it was searched for in :class:`Base2`, and so on. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 628 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 629 | In fact, it is slightly more complex than that; the method resolution order |
| 630 | changes dynamically to support cooperative calls to :func:`super`. This |
| 631 | approach is known in some other multiple-inheritance languages as |
| 632 | call-next-method and is more powerful than the super call found in |
| 633 | single-inheritance languages. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 634 | |
Georg Brandl | 85eb8c1 | 2007-08-31 16:33:38 +0000 | [diff] [blame] | 635 | Dynamic ordering is necessary because all cases of multiple inheritance exhibit |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 636 | one or more diamond relationships (where at least one of the parent classes |
Georg Brandl | 85eb8c1 | 2007-08-31 16:33:38 +0000 | [diff] [blame] | 637 | can be accessed through multiple paths from the bottommost class). For example, |
| 638 | all classes inherit from :class:`object`, so any case of multiple inheritance |
| 639 | provides more than one path to reach :class:`object`. To keep the base classes |
| 640 | from being accessed more than once, the dynamic algorithm linearizes the search |
| 641 | order in a way that preserves the left-to-right ordering specified in each |
| 642 | class, that calls each parent only once, and that is monotonic (meaning that a |
| 643 | class can be subclassed without affecting the precedence order of its parents). |
| 644 | Taken together, these properties make it possible to design reliable and |
| 645 | extensible classes with multiple inheritance. For more detail, see |
Georg Brandl | e73778c | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 646 | https://www.python.org/download/releases/2.3/mro/. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 647 | |
| 648 | |
| 649 | .. _tut-private: |
| 650 | |
| 651 | Private Variables |
| 652 | ================= |
| 653 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 654 | "Private" instance variables that cannot be accessed except from inside an |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 655 | object don't exist in Python. However, there is a convention that is followed |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 656 | by most Python code: a name prefixed with an underscore (e.g. ``_spam``) should |
| 657 | be treated as a non-public part of the API (whether it is a function, a method |
| 658 | or a data member). It should be considered an implementation detail and subject |
| 659 | to change without notice. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 660 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 661 | Since there is a valid use-case for class-private members (namely to avoid name |
| 662 | clashes of names with names defined by subclasses), there is limited support for |
| 663 | such a mechanism, called :dfn:`name mangling`. Any identifier of the form |
| 664 | ``__spam`` (at least two leading underscores, at most one trailing underscore) |
| 665 | is textually replaced with ``_classname__spam``, where ``classname`` is the |
| 666 | current class name with leading underscore(s) stripped. This mangling is done |
Georg Brandl | dffc1b8 | 2009-08-13 12:58:30 +0000 | [diff] [blame] | 667 | without regard to the syntactic position of the identifier, as long as it |
| 668 | occurs within the definition of a class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 669 | |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 670 | Name mangling is helpful for letting subclasses override methods without |
| 671 | breaking intraclass method calls. For example:: |
| 672 | |
Éric Araujo | 72db345 | 2011-07-26 16:54:24 +0200 | [diff] [blame] | 673 | class Mapping: |
| 674 | def __init__(self, iterable): |
| 675 | self.items_list = [] |
| 676 | self.__update(iterable) |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 677 | |
Éric Araujo | 72db345 | 2011-07-26 16:54:24 +0200 | [diff] [blame] | 678 | def update(self, iterable): |
| 679 | for item in iterable: |
| 680 | self.items_list.append(item) |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 681 | |
Éric Araujo | 72db345 | 2011-07-26 16:54:24 +0200 | [diff] [blame] | 682 | __update = update # private copy of original update() method |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 683 | |
Éric Araujo | 72db345 | 2011-07-26 16:54:24 +0200 | [diff] [blame] | 684 | class MappingSubclass(Mapping): |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 685 | |
Éric Araujo | 72db345 | 2011-07-26 16:54:24 +0200 | [diff] [blame] | 686 | def update(self, keys, values): |
| 687 | # provides new signature for update() |
| 688 | # but does not break __init__() |
| 689 | for item in zip(keys, values): |
| 690 | self.items_list.append(item) |
Raymond Hettinger | 6ddefd7 | 2011-06-25 16:30:39 +0200 | [diff] [blame] | 691 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 692 | Note that the mangling rules are designed mostly to avoid accidents; it still is |
| 693 | possible to access or modify a variable that is considered private. This can |
| 694 | even be useful in special circumstances, such as in the debugger. |
| 695 | |
Mark Dickinson | cf48e44 | 2010-07-12 09:37:40 +0000 | [diff] [blame] | 696 | Notice that code passed to ``exec()`` or ``eval()`` does not consider the |
| 697 | classname of the invoking class to be the current class; this is similar to the |
| 698 | effect of the ``global`` statement, the effect of which is likewise restricted |
| 699 | to code that is byte-compiled together. The same restriction applies to |
| 700 | ``getattr()``, ``setattr()`` and ``delattr()``, as well as when referencing |
| 701 | ``__dict__`` directly. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 702 | |
| 703 | |
| 704 | .. _tut-odds: |
| 705 | |
| 706 | Odds and Ends |
| 707 | ============= |
| 708 | |
| 709 | Sometimes it is useful to have a data type similar to the Pascal "record" or C |
| 710 | "struct", bundling together a few named data items. An empty class definition |
| 711 | will do nicely:: |
| 712 | |
| 713 | class Employee: |
| 714 | pass |
| 715 | |
| 716 | john = Employee() # Create an empty employee record |
| 717 | |
| 718 | # Fill the fields of the record |
| 719 | john.name = 'John Doe' |
| 720 | john.dept = 'computer lab' |
| 721 | john.salary = 1000 |
| 722 | |
| 723 | A piece of Python code that expects a particular abstract data type can often be |
| 724 | passed a class that emulates the methods of that data type instead. For |
| 725 | instance, if you have a function that formats some data from a file object, you |
Serhiy Storchaka | 91aaeac | 2013-10-09 09:54:46 +0300 | [diff] [blame] | 726 | can define a class with methods :meth:`read` and :meth:`!readline` that get the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 727 | data from a string buffer instead, and pass it as an argument. |
| 728 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 729 | .. (Unfortunately, this technique has its limitations: a class can't define |
| 730 | operations that are accessed by special syntax such as sequence subscripting |
| 731 | or arithmetic operators, and assigning such a "pseudo-file" to sys.stdin will |
| 732 | not cause the interpreter to read further input from it.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 733 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 734 | Instance method objects have attributes, too: ``m.__self__`` is the instance |
| 735 | object with the method :meth:`m`, and ``m.__func__`` is the function object |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | corresponding to the method. |
| 737 | |
| 738 | |
| 739 | .. _tut-exceptionclasses: |
| 740 | |
| 741 | Exceptions Are Classes Too |
| 742 | ========================== |
| 743 | |
| 744 | User-defined exceptions are identified by classes as well. Using this mechanism |
| 745 | it is possible to create extensible hierarchies of exceptions. |
| 746 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 747 | There are two new valid (semantic) forms for the :keyword:`raise` statement:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | |
Collin Winter | bbc9712 | 2007-09-10 00:27:23 +0000 | [diff] [blame] | 749 | raise Class |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 750 | |
Collin Winter | bbc9712 | 2007-09-10 00:27:23 +0000 | [diff] [blame] | 751 | raise Instance |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 752 | |
Collin Winter | bbc9712 | 2007-09-10 00:27:23 +0000 | [diff] [blame] | 753 | In the first form, ``Class`` must be an instance of :class:`type` or of a |
| 754 | class derived from it. The first form is a shorthand for:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 755 | |
Collin Winter | bbc9712 | 2007-09-10 00:27:23 +0000 | [diff] [blame] | 756 | raise Class() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 758 | A class in an :keyword:`except` clause is compatible with an exception if it is |
| 759 | the same class or a base class thereof (but not the other way around --- an |
| 760 | except clause listing a derived class is not compatible with a base class). For |
| 761 | example, the following code will print B, C, D in that order:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 762 | |
Georg Brandl | f5f2630 | 2008-08-08 06:50:56 +0000 | [diff] [blame] | 763 | class B(Exception): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 764 | pass |
| 765 | class C(B): |
| 766 | pass |
| 767 | class D(C): |
| 768 | pass |
| 769 | |
Georg Brandl | 52d3e7e | 2011-03-07 08:31:52 +0100 | [diff] [blame] | 770 | for cls in [B, C, D]: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 771 | try: |
Georg Brandl | 52d3e7e | 2011-03-07 08:31:52 +0100 | [diff] [blame] | 772 | raise cls() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 773 | except D: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 774 | print("D") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 775 | except C: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 776 | print("C") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 777 | except B: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 778 | print("B") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 779 | |
| 780 | Note that if the except clauses were reversed (with ``except B`` first), it |
| 781 | would have printed B, B, B --- the first matching except clause is triggered. |
| 782 | |
| 783 | When an error message is printed for an unhandled exception, the exception's |
| 784 | class name is printed, then a colon and a space, and finally the instance |
| 785 | converted to a string using the built-in function :func:`str`. |
| 786 | |
| 787 | |
| 788 | .. _tut-iterators: |
| 789 | |
| 790 | Iterators |
| 791 | ========= |
| 792 | |
| 793 | By now you have probably noticed that most container objects can be looped over |
| 794 | using a :keyword:`for` statement:: |
| 795 | |
| 796 | for element in [1, 2, 3]: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 797 | print(element) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 798 | for element in (1, 2, 3): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 799 | print(element) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 800 | for key in {'one':1, 'two':2}: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 801 | print(key) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 802 | for char in "123": |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 803 | print(char) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 804 | for line in open("myfile.txt"): |
Ezio Melotti | 79a1ffd | 2014-08-08 17:23:32 +0300 | [diff] [blame] | 805 | print(line, end='') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 806 | |
| 807 | This style of access is clear, concise, and convenient. The use of iterators |
| 808 | pervades and unifies Python. Behind the scenes, the :keyword:`for` statement |
| 809 | calls :func:`iter` on the container object. The function returns an iterator |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 810 | object that defines the method :meth:`~iterator.__next__` which accesses |
| 811 | elements in the container one at a time. When there are no more elements, |
Serhiy Storchaka | 91aaeac | 2013-10-09 09:54:46 +0300 | [diff] [blame] | 812 | :meth:`~iterator.__next__` raises a :exc:`StopIteration` exception which tells the |
| 813 | :keyword:`for` loop to terminate. You can call the :meth:`~iterator.__next__` method |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 814 | using the :func:`next` built-in function; this example shows how it all works:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 815 | |
| 816 | >>> s = 'abc' |
| 817 | >>> it = iter(s) |
| 818 | >>> it |
| 819 | <iterator object at 0x00A1DB50> |
| 820 | >>> next(it) |
| 821 | 'a' |
| 822 | >>> next(it) |
| 823 | 'b' |
| 824 | >>> next(it) |
| 825 | 'c' |
| 826 | >>> next(it) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 827 | Traceback (most recent call last): |
| 828 | File "<stdin>", line 1, in ? |
| 829 | next(it) |
| 830 | StopIteration |
| 831 | |
| 832 | Having seen the mechanics behind the iterator protocol, it is easy to add |
Georg Brandl | 0674255 | 2010-07-19 11:28:05 +0000 | [diff] [blame] | 833 | iterator behavior to your classes. Define an :meth:`__iter__` method which |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 834 | returns an object with a :meth:`~iterator.__next__` method. If the class |
| 835 | defines :meth:`__next__`, then :meth:`__iter__` can just return ``self``:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 836 | |
| 837 | class Reverse: |
Georg Brandl | da623ed | 2011-05-01 22:37:23 +0200 | [diff] [blame] | 838 | """Iterator for looping over a sequence backwards.""" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 839 | def __init__(self, data): |
| 840 | self.data = data |
| 841 | self.index = len(data) |
| 842 | def __iter__(self): |
| 843 | return self |
| 844 | def __next__(self): |
| 845 | if self.index == 0: |
| 846 | raise StopIteration |
| 847 | self.index = self.index - 1 |
| 848 | return self.data[self.index] |
| 849 | |
Georg Brandl | 2cdee70 | 2011-05-01 22:34:31 +0200 | [diff] [blame] | 850 | :: |
| 851 | |
Georg Brandl | 0674255 | 2010-07-19 11:28:05 +0000 | [diff] [blame] | 852 | >>> rev = Reverse('spam') |
| 853 | >>> iter(rev) |
| 854 | <__main__.Reverse object at 0x00A1DB50> |
| 855 | >>> for char in rev: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 856 | ... print(char) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 857 | ... |
| 858 | m |
| 859 | a |
| 860 | p |
| 861 | s |
| 862 | |
| 863 | |
| 864 | .. _tut-generators: |
| 865 | |
| 866 | Generators |
| 867 | ========== |
| 868 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 869 | :term:`Generator`\s are a simple and powerful tool for creating iterators. They |
| 870 | are written like regular functions but use the :keyword:`yield` statement |
| 871 | whenever they want to return data. Each time :func:`next` is called on it, the |
Georg Brandl | f0d2ed7 | 2014-10-31 09:29:38 +0100 | [diff] [blame] | 872 | generator resumes where it left off (it remembers all the data values and which |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 873 | statement was last executed). An example shows that generators can be trivially |
| 874 | easy to create:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 875 | |
| 876 | def reverse(data): |
| 877 | for index in range(len(data)-1, -1, -1): |
| 878 | yield data[index] |
| 879 | |
Georg Brandl | 2cdee70 | 2011-05-01 22:34:31 +0200 | [diff] [blame] | 880 | :: |
| 881 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 882 | >>> for char in reverse('golf'): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 883 | ... print(char) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 884 | ... |
| 885 | f |
| 886 | l |
| 887 | o |
Georg Brandl | 06788c9 | 2009-01-03 21:31:47 +0000 | [diff] [blame] | 888 | g |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 889 | |
Georg Brandl | f0d2ed7 | 2014-10-31 09:29:38 +0100 | [diff] [blame] | 890 | Anything that can be done with generators can also be done with class-based |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 891 | iterators as described in the previous section. What makes generators so |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 892 | compact is that the :meth:`__iter__` and :meth:`~generator.__next__` methods |
| 893 | are created automatically. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 894 | |
| 895 | Another key feature is that the local variables and execution state are |
| 896 | automatically saved between calls. This made the function easier to write and |
| 897 | much more clear than an approach using instance variables like ``self.index`` |
| 898 | and ``self.data``. |
| 899 | |
| 900 | In addition to automatic method creation and saving program state, when |
| 901 | generators terminate, they automatically raise :exc:`StopIteration`. In |
| 902 | combination, these features make it easy to create iterators with no more effort |
| 903 | than writing a regular function. |
| 904 | |
| 905 | |
| 906 | .. _tut-genexps: |
| 907 | |
| 908 | Generator Expressions |
| 909 | ===================== |
| 910 | |
| 911 | Some simple generators can be coded succinctly as expressions using a syntax |
| 912 | similar to list comprehensions but with parentheses instead of brackets. These |
| 913 | expressions are designed for situations where the generator is used right away |
| 914 | by an enclosing function. Generator expressions are more compact but less |
| 915 | versatile than full generator definitions and tend to be more memory friendly |
| 916 | than equivalent list comprehensions. |
| 917 | |
| 918 | Examples:: |
| 919 | |
| 920 | >>> sum(i*i for i in range(10)) # sum of squares |
| 921 | 285 |
| 922 | |
| 923 | >>> xvec = [10, 20, 30] |
| 924 | >>> yvec = [7, 5, 3] |
| 925 | >>> sum(x*y for x,y in zip(xvec, yvec)) # dot product |
| 926 | 260 |
| 927 | |
| 928 | >>> from math import pi, sin |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 929 | >>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 930 | |
| 931 | >>> unique_words = set(word for line in page for word in line.split()) |
| 932 | |
| 933 | >>> valedictorian = max((student.gpa, student.name) for student in graduates) |
| 934 | |
| 935 | >>> data = 'golf' |
Georg Brandl | e4ac750 | 2007-09-03 07:10:24 +0000 | [diff] [blame] | 936 | >>> list(data[i] for i in range(len(data)-1, -1, -1)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 937 | ['f', 'l', 'o', 'g'] |
| 938 | |
| 939 | |
| 940 | |
| 941 | .. rubric:: Footnotes |
| 942 | |
| 943 | .. [#] Except for one thing. Module objects have a secret read-only attribute called |
| 944 | :attr:`__dict__` which returns the dictionary used to implement the module's |
| 945 | namespace; the name :attr:`__dict__` is an attribute but not a global name. |
| 946 | Obviously, using this violates the abstraction of namespace implementation, and |
| 947 | should be restricted to things like post-mortem debuggers. |
| 948 | |