6
def change(s):
    s = s + "!"


word = "holiday"
change(word)    
print word

how come the output of this is "holiday" instead of "holiday!"? Is it because the 3 line of codes are outside the function? If so, why would it matter if it's outside the function since it's after the change function?

alicew
  • 275
  • 1
  • 2
  • 6
  • 1
    This explains your situation more generally: http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference – jdi Apr 22 '12 at 17:48

5 Answers5

6

Try this:

def change(s):
    return s + "!"

Use it like this:

word = 'holiday'
print change(word)

Or like this:

word = 'holiday'
word = change(word)
print word

Be aware that any modifications you do to a parameter string inside a function will not be seen outside the function, as the s parameter in the function is local to the scope of the function, and any changes you do (like in the code in the question) will only be visible inside the function, not outside. All function parameters in Python are passed by value.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • @alicew you're welcome! please read the link in the last paragraph of my answer to fully understand what's happening with the string inside your function. – Óscar López Apr 22 '12 at 17:47
  • @alicew also, please don't forget to accept as correct (click the check mark to its left) the answer that was most helpful – Óscar López Apr 22 '12 at 17:49
3

You are only modifying the local variable s in the function change. This does not modify the variable word. If you wish to modify word, you need to return the result of change and assign it to word:

def change(s):
    s = s + "!"
    return s


word = "holiday"
word = change(word)    
print word
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
1

You aren't returning the value from change.

Try:

def change(s):
    s = s + "!"
    return s


word = "holiday"
word = change(word)    
print word

So in the above example, s is being modified, and then word is reassigned the modified value.

Niall Byrne
  • 2,448
  • 1
  • 17
  • 18
1

You are passing a 'copy' of word rather that word instance to the function. Therefore changes are applied to the copy of word you hold in s variable inside the function

jbreicis
  • 588
  • 2
  • 8
  • 1
    Thats not true. python passes a reference to the value. He is just shadowing the variable with a local one – jdi Apr 22 '12 at 17:46
  • Thats more correct explanation; but in given scenario it helps to explain that as a result of shadowing and different scopes, it appears that you are working on a copy – jbreicis Apr 22 '12 at 17:50
  • I agree that your answer is a simplification of the situation, but I still think its misleading to use the word copy. They are the same object inside and outside of the function. A string is mutable so there is no way to change it other than shadowing it and returning a new copy. – jdi Apr 22 '12 at 17:59
  • 2
    The "copy" happens in s = s + "!", not in the function call. Local s and global word refer to the same string before that line; after that line, s refers to a modified copy "holiday!". Your answer as written is dangerously misleading in Python, which is not a call-by-value language. – Russell Borogove Apr 22 '12 at 18:09
  • @jdi: I'm assuming you mean "a string is immutable" ;) – Joel Cornett Apr 22 '12 at 18:12
  • I know I ovsdsimplified to the point of potentially misleading. Feel free to downvote. – jbreicis Apr 22 '12 at 18:17
  • @JoelCornett: Gah TYPO. IMmutable. And I wont downvote this. It was a comment-only situation :-) – jdi Apr 22 '12 at 18:21
1

s in change(s) is a name that is bound to the value of word when you call change(word).

In the line s = s + "!" you are binding s to a new string which is created by concatenating the value bound to s and !, not doing anything to word.

Also it is best not to have side effects even if it is possible so returning a new value is better.

def change(s):
    return s + '!'
jamylak
  • 128,818
  • 30
  • 231
  • 230