Python 和 Java 参数传递 [英] Python and Java parameter passing

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

问题描述

我在几个地方看到过,包括 Python 使用的通过赋值"语义传递的 Python 文档.来自 Java 背景,其中说Java 按值传递原语,对象按引用传递"的常见错误是因为对象引用按值传递,我不禁想知道 Python 是否真的在做同样的事情东西.

I've seen in several places, including the Python documentation that Python uses pass by "assignment" semantics. Coming from a Java background, where the common mistake of saying "Java passes primitives by value, and objects by reference" is as a result of having objects references passed by value, I can't help but wonder if Python is really doing the same thing.

对我来说,通过值传递对象引用和通过赋值传递的概念似乎是相同的.Python 使用pass-by-assignment"一词是为了缓解我上面描述的错误语句的问题吗?如果是这样,可以说参数传递在两种语言中的工作方式相似吗?

To me, the concepts of passing object references by value and pass by assignment seem identical. Is Python's use of the term "pass-by-assignment" an attempt to mitigate the problem of having erroneous statements such as the one I described above? If so, is it fair to say that parameter passing works in a similar way in the two languages?

我不认为这个问题是重复的.在这里,我询问 Python 使用的术语,并直接参考 Java 的工作方式.另一个问题是关于语言是按值传递还是按引用传递.我知道传递赋值是这里使用的命名法,但我的论点似乎得到了公认的答案的支持,即这与 Java 的工作方式确实没有什么不同;这只是一个不同的名字.

I don't think that this question is a duplicate. Here I'm asking about the terminology used by Python, with direct reference to how Java does things. The other question is about whether the language is pass-by-value or pass-by-reference. I know that pass by assignment is the nomenclature used here, but my contention, which seems to be supported by the accepted answer, is that this is really no different to how Java does things; it's just a different name.

推荐答案

是的,Python 在这方面似乎等同于 Java.来自 http://learnpython.pbworks.com/w/page/15956522/Assignment:

Yes Python seems to be equivalent to Java in this respect. From http://learnpython.pbworks.com/w/page/15956522/Assignment :

但是您必须小心更改为参数".为参数名称分配一个新值(在函数,参数只是一个局部变量)不会改变原始对象——它只是将局部变量重新绑定到一个新对象.

But you must be careful about what is meant by "changes to parameters". Assigning a new value to a parameter name (inside the function, a parameter is just a local variable) does not change the original object--it only rebinds the local variable to a new object.

因此,Python按值"传递对象引用".

So, Python passes "object references" "by value".

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

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