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