Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

Parameter Passing and Return Values

When a function is invoked, its parameters are passed by reference. If a mutable object (such as a list or dictionary) is passed to a function where it’s then modified, those changes will be reflected in the caller. For example:

a = [1,2,3,4,5]
def foo(x):
    x[3] = -55    # Modify an element of x

foo(a)            # Pass a
print a           # Produces [1,2,3,-55,5]

The return statement returns a value from a function. If no value is specified or you omit the return statement, the None object is returned. To return multiple values, place them in a tuple:

def factor(a):
    d = 2
    while (d <= (a/2)):
        if ((a/d)*d == a):
              return ((a/d),d)
        d = d + 1
    return (a,1)

Multiple return values returned in a tuple can be assigned to individual variables: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book