Python. Parameters and returned values

混江龙づ霸主 提交于 2020-01-21 11:08:06

问题


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:


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.



来源:https://stackoverflow.com/questions/43204558/python-parameters-and-returned-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!