Python.参数和返回值 [英] Python. Parameters and returned values

查看:54
本文介绍了Python.参数和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中我对 Python 比较陌生,我可能会使用一些不正确的术语,并且会产生误解 - 这就是我在这里的原因.

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.

我编写了这个简单的函数来对列表中的项目进行排序

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)

我已为列表赋值

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

然后我调用函数

func_sort_list(unsort_letters)

得到结果

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']

我想要的结果是什么.

以下对发生的事情的解释是否正确,以便我在编写函数时感觉更安全一些?

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

  1. unsort_letters 的原始值 ['q','w','e', ...] 是全局的"吗?

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

通过调用 func_sort_list(unsort_letters) 我已将 unsort_letters 的地址/指针传递给 func_sort_list?

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

NN 是函数的局部"变量,但它包含指向传递变量的指针/地址,并且由于列表是可变的,因此函数内 unsort_letters 的内容正在更改?

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?

这导致我:

  1. 是否有过无法更改函数内传递参数的内容而不得不编写如下内容的情况?

  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)

我必须按如下方式调用以获取 var_s 中返回值的值.

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>>)

?

推荐答案

您的心智模型非常正确.但是,您不必担心 Python 是按值传递"还是按引用传递".你必须学习 Python 中的赋值是如何工作的.无疑是最好的资源.要知道的最重要的事实是赋值永远不会复制数据.

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

您传入了 unsort_letters 作为参数.您为该参数指定另一个名称 (NN) - 就是这样,没有数据被复制.

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).

但是需要注意的是,Python 在赋值和参数传递期间不会区别对待可变类型和不可变类型.你根本无法改变不可变类型,因为它们没有改变方法.

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.

这篇关于Python.参数和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆