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?
The original value of unsort_letters , ['q','w','e', ...], is "global"?
By calling func_sort_list(unsort_letters) I have passed the address / pointer of unsort_letters to func_sort_list?
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:
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>>)
?
Introduction. A function can take parameters to use in its code, and it can return zero or more values (when more values are returned, one of the values often speaks of a tuple of values).
In general, a function takes arguments (if any), performs some operations, and returns a value (or object). The value that a function returns to the caller is generally known as the function's return value. All Python functions have a return value, either explicit or implicit.
Goal: Parameters and return values allow students to write programs that are more organized and cleaner. Naming functions helps students write programs that read more like descriptions of what they do, and they also help students reuse code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With