7

Being relatively new to Python in this question I may use some incorrect terminology and will display misunderstanding - which I why I am here.

I am studying Python functions and am trying to ensure I understand how variables are passed and returned.

I have written this trivial function to sort items in a list

def func_sort_list(NN):
    for w in NN:
        print (w, type(w))

    NN.sort()
    print (NN)

    return (w)

I have assigned values to a list

unsort_letters=['q','w','e','r','t','y']

Then I envoke the function

func_sort_list(unsort_letters)

and get the result

q <class 'str'>
w <class 'str'>
e <class 'str'>
r <class 'str'>
t <class 'str'>
y <class 'str'>
['e', 'q', 'r', 't', 'w', 'y']
'y'

then, after execution, if I display the contents of the variable I originally passed to the function I get

unsort_letters
['e', 'q', 'r', 't', 'w', 'y']

Which is the result I want.

Is the following interpretation of what happened correct so I can feel a bit more secure when writing functions?

  1. The original value of unsort_letters , ['q','w','e', ...], is "global"?

  2. By calling func_sort_list(unsort_letters) I have passed the address / pointer of unsort_letters to func_sort_list?

  3. NN is a variable "local" to the function but it contains the pointer/address to the passed variable and since a list is mutable the contents of unsort_letters is being changed within the function?

Which leads me to:

  1. Is there ever a circumstance when I cannot change the contents of the passed parameter within the function and I have to write something like the following?

    def func_return_only(input_parm):
        result_var = << some processing on input_parm goes here>>
    
    return (result_var)
    

Which I have to envoke as follows to get at the value of the returned values in var_s.

var_s = func_return_only(<< my input variable or value>>)

?

1
  • This example in the documentation explains the issue well. Commented Apr 4, 2017 at 10:49

1 Answer 1

6

Your mental model is pretty much correct. However, you should not worry about whether Python is "pass by value" or "pass by reference". You have to learn how assignment in Python works. This is hands down the best resource to do so. The most important fact to know is that assignment never copies data.

Once you understand assignment, the only thing you have to know is that function parameters are passed by assignment.

The first thing that happens implicitly when you call your function is

NN = unsort_letters

You passed in unsort_letters as the argument. You assign another name (NN) to that argument - and that's it, no data is copied.

Is there ever a circumstance when I cannot change the contents of the passed parameter within the function

Yes, when whatever you pass in is immutable. Integers, for example, have no methods to update their value, so you can't mutate them in the function body (or anywhere else).

It is important to note however that Python does not treat mutable and immutable types differently during assignment and parameter passing. You simply cannot mutate immutable types because they have no mutating methods on them.

Sign up to request clarification or add additional context in comments.

1 Comment

Quote "You passed in unsort_letters as the argument. You assign another name (NN) to that argument - and that's it, no data is copied." That makes complete sense. Thanks for theYoutube link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.