blob: f57e9e9780ad09d27a549cee9a248f92b2aa6e9a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-morecontrol:
2
3***********************
4More Control Flow Tools
5***********************
6
7Besides the :keyword:`while` statement just introduced, Python knows the usual
8control flow statements known from other languages, with some twists.
9
10
11.. _tut-if:
12
13:keyword:`if` Statements
14========================
15
16Perhaps the most well-known statement type is the :keyword:`if` statement. For
17example::
18
19 >>> x = int(raw_input("Please enter an integer: "))
Georg Brandl3ce0dee2008-09-13 17:18:11 +000020 Please enter an integer: 42
Georg Brandl8ec7f652007-08-15 14:28:01 +000021 >>> if x < 0:
22 ... x = 0
23 ... print 'Negative changed to zero'
24 ... elif x == 0:
25 ... print 'Zero'
26 ... elif x == 1:
27 ... print 'Single'
28 ... else:
29 ... print 'More'
Georg Brandl3ce0dee2008-09-13 17:18:11 +000030 ...
31 More
Georg Brandl8ec7f652007-08-15 14:28:01 +000032
33There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is
34optional. The keyword ':keyword:`elif`' is short for 'else if', and is useful
35to avoid excessive indentation. An :keyword:`if` ... :keyword:`elif` ...
Georg Brandlb19be572007-12-29 10:57:00 +000036:keyword:`elif` ... sequence is a substitute for the ``switch`` or
37``case`` statements found in other languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +000038
39
40.. _tut-for:
41
42:keyword:`for` Statements
43=========================
44
45.. index::
46 statement: for
47 statement: for
48
49The :keyword:`for` statement in Python differs a bit from what you may be used
50to in C or Pascal. Rather than always iterating over an arithmetic progression
51of numbers (like in Pascal), or giving the user the ability to define both the
52iteration step and halting condition (as C), Python's :keyword:`for` statement
53iterates over the items of any sequence (a list or a string), in the order that
54they appear in the sequence. For example (no pun intended):
55
Georg Brandlb19be572007-12-29 10:57:00 +000056.. One suggestion was to give a real C example here, but that may only serve to
57 confuse non-C programmers.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
59::
60
61 >>> # Measure some strings:
62 ... a = ['cat', 'window', 'defenestrate']
63 >>> for x in a:
64 ... print x, len(x)
65 ...
66 cat 3
67 window 6
68 defenestrate 12
69
70It is not safe to modify the sequence being iterated over in the loop (this can
71only happen for mutable sequence types, such as lists). If you need to modify
72the list you are iterating over (for example, to duplicate selected items) you
73must iterate over a copy. The slice notation makes this particularly
74convenient::
75
76 >>> for x in a[:]: # make a slice copy of the entire list
77 ... if len(x) > 6: a.insert(0, x)
78 ...
79 >>> a
80 ['defenestrate', 'cat', 'window', 'defenestrate']
81
82
83.. _tut-range:
84
85The :func:`range` Function
86==========================
87
88If you do need to iterate over a sequence of numbers, the built-in function
89:func:`range` comes in handy. It generates lists containing arithmetic
90progressions::
91
92 >>> range(10)
93 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
94
95The given end point is never part of the generated list; ``range(10)`` generates
96a list of 10 values, the legal indices for items of a sequence of length 10. It
97is possible to let the range start at another number, or to specify a different
98increment (even negative; sometimes this is called the 'step')::
99
100 >>> range(5, 10)
101 [5, 6, 7, 8, 9]
102 >>> range(0, 10, 3)
103 [0, 3, 6, 9]
104 >>> range(-10, -100, -30)
105 [-10, -40, -70]
106
Georg Brandlfa71a902008-12-05 09:08:28 +0000107To iterate over the indices of a sequence, you can combine :func:`range` and
108:func:`len` as follows::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
110 >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
111 >>> for i in range(len(a)):
112 ... print i, a[i]
113 ...
114 0 Mary
115 1 had
116 2 a
117 3 little
118 4 lamb
119
Georg Brandlfa71a902008-12-05 09:08:28 +0000120In most such cases, however, it is convenient to use the :func:`enumerate`
121function, see :ref:`tut-loopidioms`.
122
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123
124.. _tut-break:
125
126:keyword:`break` and :keyword:`continue` Statements, and :keyword:`else` Clauses on Loops
127=========================================================================================
128
129The :keyword:`break` statement, like in C, breaks out of the smallest enclosing
130:keyword:`for` or :keyword:`while` loop.
131
132The :keyword:`continue` statement, also borrowed from C, continues with the next
133iteration of the loop.
134
135Loop statements may have an ``else`` clause; it is executed when the loop
136terminates through exhaustion of the list (with :keyword:`for`) or when the
137condition becomes false (with :keyword:`while`), but not when the loop is
138terminated by a :keyword:`break` statement. This is exemplified by the
139following loop, which searches for prime numbers::
140
141 >>> for n in range(2, 10):
142 ... for x in range(2, n):
143 ... if n % x == 0:
144 ... print n, 'equals', x, '*', n/x
145 ... break
Benjamin Peterson80790282008-08-02 03:05:11 +0000146 ... else:
147 ... # loop fell through without finding a factor
148 ... print n, 'is a prime number'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149 ...
150 2 is a prime number
151 3 is a prime number
152 4 equals 2 * 2
153 5 is a prime number
154 6 equals 2 * 3
155 7 is a prime number
156 8 equals 2 * 4
157 9 equals 3 * 3
158
159
160.. _tut-pass:
161
162:keyword:`pass` Statements
163==========================
164
165The :keyword:`pass` statement does nothing. It can be used when a statement is
166required syntactically but the program requires no action. For example::
167
168 >>> while True:
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000169 ... pass # Busy-wait for keyboard interrupt (Ctrl+C)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000170 ...
171
Georg Brandl4aef7032008-11-07 08:56:27 +0000172This is commonly used for creating minimal classes such as exceptions, or
173for ignoring unwanted exceptions::
174
175 >>> class ParserError(Exception):
176 ... pass
177 ...
178 >>> try:
179 ... import audioop
180 ... except ImportError:
181 ... pass
182 ...
183
184Another place :keyword:`pass` can be used is as a place-holder for a function or
185conditional body when you are working on new code, allowing you to keep
186thinking at a more abstract level. However, as :keyword:`pass` is silently
187ignored, a better choice may be to raise a :exc:`NotImplementedError`
188exception::
189
190 >>> def initlog(*args):
191 ... raise NotImplementedError # Open logfile if not already open
192 ... if not logfp:
193 ... raise NotImplementedError # Set up dummy log back-end
194 ... raise NotImplementedError('Call log initialization handler')
195 ...
196
197If :keyword:`pass` were used here and you later ran tests, they may fail
198without indicating why. Using :exc:`NotImplementedError` causes this code
199to raise an exception, telling you exactly where the incomplete code
200is. Note the two calling styles of the exceptions above.
201The first style, with no message but with an accompanying comment,
202lets you easily leave the comment when you remove the exception,
203which ideally would be a good description for
204the block of code the exception is a placeholder for. However, the
205third example, providing a message for the exception, will produce
206a more useful traceback.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
208.. _tut-functions:
209
210Defining Functions
211==================
212
213We can create a function that writes the Fibonacci series to an arbitrary
214boundary::
215
216 >>> def fib(n): # write Fibonacci series up to n
217 ... """Print a Fibonacci series up to n."""
218 ... a, b = 0, 1
219 ... while b < n:
220 ... print b,
221 ... a, b = b, a+b
222 ...
223 >>> # Now call the function we just defined:
224 ... fib(2000)
225 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
226
227.. index::
228 single: documentation strings
229 single: docstrings
230 single: strings, documentation
231
232The keyword :keyword:`def` introduces a function *definition*. It must be
233followed by the function name and the parenthesized list of formal parameters.
234The statements that form the body of the function start at the next line, and
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000235must be indented.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000237The first statement of the function body can optionally be a string literal;
238this string literal is the function's documentation string, or :dfn:`docstring`.
239(More about docstrings can be found in the section :ref:`tut-docstrings`.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000240There are tools which use docstrings to automatically produce online or printed
241documentation, or to let the user interactively browse through code; it's good
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000242practice to include docstrings in code that you write, so make a habit of it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243
244The *execution* of a function introduces a new symbol table used for the local
245variables of the function. More precisely, all variable assignments in a
246function store the value in the local symbol table; whereas variable references
Georg Brandlaa0de3f2008-01-21 16:51:51 +0000247first look in the local symbol table, then in the local symbol tables of
248enclosing functions, then in the global symbol table, and finally in the table
249of built-in names. Thus, global variables cannot be directly assigned a value
250within a function (unless named in a :keyword:`global` statement), although they
251may be referenced.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000252
253The actual parameters (arguments) to a function call are introduced in the local
254symbol table of the called function when it is called; thus, arguments are
255passed using *call by value* (where the *value* is always an object *reference*,
256not the value of the object). [#]_ When a function calls another function, a new
257local symbol table is created for that call.
258
259A function definition introduces the function name in the current symbol table.
260The value of the function name has a type that is recognized by the interpreter
261as a user-defined function. This value can be assigned to another name which
262can then also be used as a function. This serves as a general renaming
263mechanism::
264
265 >>> fib
266 <function fib at 10042ed0>
267 >>> f = fib
268 >>> f(100)
269 1 1 2 3 5 8 13 21 34 55 89
270
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000271Coming from other languages, you might object that ``fib`` is not a function but
272a procedure since it doesn't return a value. In fact, even functions without a
273:keyword:`return` statement do return a value, albeit a rather boring one. This
274value is called ``None`` (it's a built-in name). Writing the value ``None`` is
275normally suppressed by the interpreter if it would be the only value written.
276You can see it if you really want to using :keyword:`print`::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000277
Georg Brandl706132b2007-10-30 17:57:12 +0000278 >>> fib(0)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279 >>> print fib(0)
280 None
281
282It is simple to write a function that returns a list of the numbers of the
283Fibonacci series, instead of printing it::
284
285 >>> def fib2(n): # return Fibonacci series up to n
286 ... """Return a list containing the Fibonacci series up to n."""
287 ... result = []
288 ... a, b = 0, 1
289 ... while b < n:
290 ... result.append(b) # see below
291 ... a, b = b, a+b
292 ... return result
293 ...
294 >>> f100 = fib2(100) # call it
295 >>> f100 # write the result
296 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
297
298This example, as usual, demonstrates some new Python features:
299
300* The :keyword:`return` statement returns with a value from a function.
301 :keyword:`return` without an expression argument returns ``None``. Falling off
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000302 the end of a function also returns ``None``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000303
304* The statement ``result.append(b)`` calls a *method* of the list object
305 ``result``. A method is a function that 'belongs' to an object and is named
306 ``obj.methodname``, where ``obj`` is some object (this may be an expression),
307 and ``methodname`` is the name of a method that is defined by the object's type.
308 Different types define different methods. Methods of different types may have
309 the same name without causing ambiguity. (It is possible to define your own
310 object types and methods, using *classes*, as discussed later in this tutorial.)
311 The method :meth:`append` shown in the example is defined for list objects; it
312 adds a new element at the end of the list. In this example it is equivalent to
313 ``result = result + [b]``, but more efficient.
314
315
316.. _tut-defining:
317
318More on Defining Functions
319==========================
320
321It is also possible to define functions with a variable number of arguments.
322There are three forms, which can be combined.
323
324
325.. _tut-defaultargs:
326
327Default Argument Values
328-----------------------
329
330The most useful form is to specify a default value for one or more arguments.
331This creates a function that can be called with fewer arguments than it is
332defined to allow. For example::
333
334 def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
335 while True:
336 ok = raw_input(prompt)
337 if ok in ('y', 'ye', 'yes'): return True
338 if ok in ('n', 'no', 'nop', 'nope'): return False
339 retries = retries - 1
340 if retries < 0: raise IOError, 'refusenik user'
341 print complaint
342
343This function can be called either like this: ``ask_ok('Do you really want to
344quit?')`` or like this: ``ask_ok('OK to overwrite the file?', 2)``.
345
346This example also introduces the :keyword:`in` keyword. This tests whether or
347not a sequence contains a certain value.
348
349The default values are evaluated at the point of function definition in the
350*defining* scope, so that ::
351
352 i = 5
353
354 def f(arg=i):
355 print arg
356
357 i = 6
358 f()
359
360will print ``5``.
361
362**Important warning:** The default value is evaluated only once. This makes a
363difference when the default is a mutable object such as a list, dictionary, or
364instances of most classes. For example, the following function accumulates the
365arguments passed to it on subsequent calls::
366
367 def f(a, L=[]):
368 L.append(a)
369 return L
370
371 print f(1)
372 print f(2)
373 print f(3)
374
375This will print ::
376
377 [1]
378 [1, 2]
379 [1, 2, 3]
380
381If you don't want the default to be shared between subsequent calls, you can
382write the function like this instead::
383
384 def f(a, L=None):
385 if L is None:
386 L = []
387 L.append(a)
388 return L
389
390
391.. _tut-keywordargs:
392
393Keyword Arguments
394-----------------
395
396Functions can also be called using keyword arguments of the form ``keyword =
397value``. For instance, the following function::
398
399 def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
400 print "-- This parrot wouldn't", action,
401 print "if you put", voltage, "volts through it."
402 print "-- Lovely plumage, the", type
403 print "-- It's", state, "!"
404
405could be called in any of the following ways::
406
407 parrot(1000)
408 parrot(action = 'VOOOOOM', voltage = 1000000)
409 parrot('a thousand', state = 'pushing up the daisies')
410 parrot('a million', 'bereft of life', 'jump')
411
412but the following calls would all be invalid::
413
414 parrot() # required argument missing
415 parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
416 parrot(110, voltage=220) # duplicate value for argument
417 parrot(actor='John Cleese') # unknown keyword
418
419In general, an argument list must have any positional arguments followed by any
420keyword arguments, where the keywords must be chosen from the formal parameter
421names. It's not important whether a formal parameter has a default value or
422not. No argument may receive a value more than once --- formal parameter names
423corresponding to positional arguments cannot be used as keywords in the same
424calls. Here's an example that fails due to this restriction::
425
426 >>> def function(a):
427 ... pass
428 ...
429 >>> function(0, a=0)
430 Traceback (most recent call last):
431 File "<stdin>", line 1, in ?
432 TypeError: function() got multiple values for keyword argument 'a'
433
434When a final formal parameter of the form ``**name`` is present, it receives a
435dictionary (see :ref:`typesmapping`) containing all keyword arguments except for
436those corresponding to a formal parameter. This may be combined with a formal
437parameter of the form ``*name`` (described in the next subsection) which
438receives a tuple containing the positional arguments beyond the formal parameter
439list. (``*name`` must occur before ``**name``.) For example, if we define a
440function like this::
441
442 def cheeseshop(kind, *arguments, **keywords):
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000443 print "-- Do you have any", kind, "?"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000444 print "-- I'm sorry, we're all out of", kind
445 for arg in arguments: print arg
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000446 print "-" * 40
Georg Brandl8ec7f652007-08-15 14:28:01 +0000447 keys = keywords.keys()
448 keys.sort()
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000449 for kw in keys: print kw, ":", keywords[kw]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
451It could be called like this::
452
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000453 cheeseshop("Limburger", "It's very runny, sir.",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000454 "It's really very, VERY runny, sir.",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000455 shopkeeper='Michael Palin',
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000456 client="John Cleese",
457 sketch="Cheese Shop Sketch")
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458
459and of course it would print::
460
461 -- Do you have any Limburger ?
462 -- I'm sorry, we're all out of Limburger
463 It's very runny, sir.
464 It's really very, VERY runny, sir.
465 ----------------------------------------
466 client : John Cleese
467 shopkeeper : Michael Palin
468 sketch : Cheese Shop Sketch
469
470Note that the :meth:`sort` method of the list of keyword argument names is
471called before printing the contents of the ``keywords`` dictionary; if this is
472not done, the order in which the arguments are printed is undefined.
473
474
475.. _tut-arbitraryargs:
476
477Arbitrary Argument Lists
478------------------------
479
Andrew M. Kuchling3822af62008-04-15 13:10:07 +0000480.. index::
481 statement: *
482
Georg Brandl8ec7f652007-08-15 14:28:01 +0000483Finally, the least frequently used option is to specify that a function can be
484called with an arbitrary number of arguments. These arguments will be wrapped
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000485up in a tuple (see :ref:`tut-tuples`). Before the variable number of arguments,
486zero or more normal arguments may occur. ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000487
Benjamin Petersondee01d82008-05-28 11:51:41 +0000488 def write_multiple_items(file, separator, *args):
489 file.write(separator.join(args))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000490
491
492.. _tut-unpacking-arguments:
493
494Unpacking Argument Lists
495------------------------
496
497The reverse situation occurs when the arguments are already in a list or tuple
498but need to be unpacked for a function call requiring separate positional
499arguments. For instance, the built-in :func:`range` function expects separate
500*start* and *stop* arguments. If they are not available separately, write the
501function call with the ``*``\ -operator to unpack the arguments out of a list
502or tuple::
503
504 >>> range(3, 6) # normal call with separate arguments
505 [3, 4, 5]
506 >>> args = [3, 6]
507 >>> range(*args) # call with arguments unpacked from a list
508 [3, 4, 5]
509
Andrew M. Kuchling3822af62008-04-15 13:10:07 +0000510.. index::
511 statement: **
512
Georg Brandl8ec7f652007-08-15 14:28:01 +0000513In the same fashion, dictionaries can deliver keyword arguments with the ``**``\
514-operator::
515
516 >>> def parrot(voltage, state='a stiff', action='voom'):
517 ... print "-- This parrot wouldn't", action,
518 ... print "if you put", voltage, "volts through it.",
519 ... print "E's", state, "!"
520 ...
521 >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
522 >>> parrot(**d)
523 -- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
524
525
526.. _tut-lambda:
527
528Lambda Forms
529------------
530
531By popular demand, a few features commonly found in functional programming
532languages like Lisp have been added to Python. With the :keyword:`lambda`
533keyword, small anonymous functions can be created. Here's a function that
534returns the sum of its two arguments: ``lambda a, b: a+b``. Lambda forms can be
535used wherever function objects are required. They are syntactically restricted
536to a single expression. Semantically, they are just syntactic sugar for a
537normal function definition. Like nested function definitions, lambda forms can
538reference variables from the containing scope::
539
540 >>> def make_incrementor(n):
541 ... return lambda x: x + n
542 ...
543 >>> f = make_incrementor(42)
544 >>> f(0)
545 42
546 >>> f(1)
547 43
548
549
550.. _tut-docstrings:
551
552Documentation Strings
553---------------------
554
555.. index::
556 single: docstrings
557 single: documentation strings
558 single: strings, documentation
559
560There are emerging conventions about the content and formatting of documentation
561strings.
562
563The first line should always be a short, concise summary of the object's
564purpose. For brevity, it should not explicitly state the object's name or type,
565since these are available by other means (except if the name happens to be a
566verb describing a function's operation). This line should begin with a capital
567letter and end with a period.
568
569If there are more lines in the documentation string, the second line should be
570blank, visually separating the summary from the rest of the description. The
571following lines should be one or more paragraphs describing the object's calling
572conventions, its side effects, etc.
573
574The Python parser does not strip indentation from multi-line string literals in
575Python, so tools that process documentation have to strip indentation if
576desired. This is done using the following convention. The first non-blank line
577*after* the first line of the string determines the amount of indentation for
578the entire documentation string. (We can't use the first line since it is
579generally adjacent to the string's opening quotes so its indentation is not
580apparent in the string literal.) Whitespace "equivalent" to this indentation is
581then stripped from the start of all lines of the string. Lines that are
582indented less should not occur, but if they occur all their leading whitespace
583should be stripped. Equivalence of whitespace should be tested after expansion
584of tabs (to 8 spaces, normally).
585
586Here is an example of a multi-line docstring::
587
588 >>> def my_function():
589 ... """Do nothing, but document it.
590 ...
591 ... No, really, it doesn't do anything.
592 ... """
593 ... pass
594 ...
595 >>> print my_function.__doc__
596 Do nothing, but document it.
597
598 No, really, it doesn't do anything.
599
600
Georg Brandl35f88612008-01-06 22:05:40 +0000601.. _tut-codingstyle:
602
603Intermezzo: Coding Style
604========================
605
606.. sectionauthor:: Georg Brandl <georg@python.org>
607.. index:: pair: coding; style
608
609Now that you are about to write longer, more complex pieces of Python, it is a
610good time to talk about *coding style*. Most languages can be written (or more
611concise, *formatted*) in different styles; some are more readable than others.
612Making it easy for others to read your code is always a good idea, and adopting
613a nice coding style helps tremendously for that.
614
Andrew M. Kuchling8c65b1e2008-04-15 13:10:41 +0000615For Python, :pep:`8` has emerged as the style guide that most projects adhere to;
Georg Brandl35f88612008-01-06 22:05:40 +0000616it promotes a very readable and eye-pleasing coding style. Every Python
617developer should read it at some point; here are the most important points
618extracted for you:
619
620* Use 4-space indentation, and no tabs.
621
622 4 spaces are a good compromise between small indentation (allows greater
623 nesting depth) and large indentation (easier to read). Tabs introduce
624 confusion, and are best left out.
625
626* Wrap lines so that they don't exceed 79 characters.
627
628 This helps users with small displays and makes it possible to have several
629 code files side-by-side on larger displays.
630
631* Use blank lines to separate functions and classes, and larger blocks of
632 code inside functions.
633
634* When possible, put comments on a line of their own.
635
636* Use docstrings.
637
638* Use spaces around operators and after commas, but not directly inside
639 bracketing constructs: ``a = f(1, 2) + g(3, 4)``.
640
641* Name your classes and functions consistently; the convention is to use
642 ``CamelCase`` for classes and ``lower_case_with_underscores`` for functions
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000643 and methods. Always use ``self`` as the name for the first method argument
644 (see :ref:`tut-firstclasses` for more on classes and methods).
Georg Brandl35f88612008-01-06 22:05:40 +0000645
646* Don't use fancy encodings if your code is meant to be used in international
647 environments. Plain ASCII works best in any case.
648
Georg Brandl8ec7f652007-08-15 14:28:01 +0000649
650.. rubric:: Footnotes
651
Georg Brandl35f88612008-01-06 22:05:40 +0000652.. [#] Actually, *call by object reference* would be a better description,
653 since if a mutable object is passed, the caller will see any changes the
654 callee makes to it (items inserted into a list).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000655