Skip to content

Conversation

@jaredberghold
Copy link

The exercise looks at what happens when immutable objects are passed into a function. People without previous programming experience might assume that it's possible to swap 2 numbers using a function written like this. And people with limited programming experience in C or Java (or C-like languages) might see the example code and wonder if, when it comes to primitive types, Python is pass-by-value or pass-by-reference. The answer is that Python is neither. This exercise provides the opportunity to have a discussion about this particular part of the language.

There's a fairly good discussion of how arguments are passed into functions in Python here: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ and the Python docs on how to deal with the problem outlined by this exercise are here: https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference

Arguably this exercise introduces new concepts, however, I feel that it is an important "gotcha" to be aware of. The exercise could be seen to build on the discussion in the section titled "Ch-Ch-Ch-Changes" in 03-lists.html by showing how immutable objects behave when passed into a function. The exercise could be extended to get attendees to rewrite the "swap" function so that it will work as intended. An example answer would be:

a = 3
b = 7

def swap(a, b):
    temp = a
    a = b
    b = temp
    return a, b

a, b = swap(a, b)

print(a, b)

The exercise looks at what happens when immutable objects are passed into a function. People without previous programming experience might assume that it's possible to swap 2 numbers using a function written like this. And people with limited programming experience in C or Java (or C-like languages) might see the example code and wonder if, when it comes to primitive types, Python is pass-by-value or pass-by-reference. The answer is that Python is neither. This exercise provides the opportunity to have a discussion about this particular part of the language.

There's a fairly good discussion of how arguments are passed into functions in Python here: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ and the Python docs on how to deal with the problem outlined by this exercise are here: https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference

Arguably this exercise introduces new concepts, however, I feel that it is an important "gotcha" to be aware of. The exercise could be seen to build on the discussion in the section titled "Ch-Ch-Ch-Changes" in 03-lists.html by showing how immutable objects behave when passed into a function. The exercise could be extended to get attendees to rewrite the "swap" function so that it will work as intended. An example answer would be:

a = 3
b = 7

def swap(a, b):
    temp = a
    a = b
    b = temp
    return a, b

a, b = swap(a, b)

print(a, b)
@jaredberghold jaredberghold changed the title Suggesting new exercise for the Creating Functions page. Suggesting new exercise for the Creating Functions page Mar 16, 2016
@valentina-s valentina-s merged commit 5930b43 into swcarpentry:gh-pages Jun 21, 2016
rgaiacs pushed a commit to rgaiacs/swc-python-novice-inflammation that referenced this pull request May 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants