简体   繁体   English

蟒蛇。 参数和返回值

[英]Python. Parameters and returned values

Being relatively new to Python in this question I may use some incorrect terminology and will display misunderstanding - which I why I am here. 在这个问题上相对较新的Python,我可能会使用一些不正确的术语,并会显示出误解 - 这就是我在这里的原因。

I am studying Python functions and am trying to ensure I understand how variables are passed and returned. 我正在研究Python函数,并试图确保我理解变量的传递和返回方式。

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"? unsort_letters的原始值['q','w','e',...]是“全局的”?

  2. By calling func_sort_list(unsort_letters) I have passed the address / pointer of unsort_letters to func_sort_list? 通过调用func_sort_list(unsort_letters)我已经将unsort_letters的地址/指针传递给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? NN是函数的变量“local”,但它包含传递变量的指针/地址,因为列表是可变的,unsort_letters的内容在函数内被更改?

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中返回值的值。

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

?

Your mental model is pretty much correct. 你的心理模型非常正确。 However, you should not worry about whether Python is "pass by value" or "pass by reference". 但是,您不应该担心Python是“按值传递”还是“按引用传递”。 You have to learn how assignment in Python works. 您必须了解Python中的赋值如何工作。 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. 你传入unsort_letters作为参数。 You assign another name ( NN ) to that argument - and that's it, no data is copied. 您为该参数指定另一个名称( NN ) - 就是这样,不会复制任何数据。

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. 然而,重要的是要注意Python在赋值和参数传递期间不会以不同方式处理可变和不可变类型。 You simply cannot mutate immutable types because they have no mutating methods on them. 你根本无法改变不可变类型,因为它们没有变异方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM