Python:按值调用和按对象调用有什么区别? [英] Python: What is the difference between Call-by-Value and Call-by-Object?

查看:28
本文介绍了Python:按值调用和按对象调用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人说在 Python 中,函数的参数是使用按值调用模型传递的.据我了解,它实际上不是一种按值调用的语言,而是一种按对象调用或按共享调用的模型.

Many people say that in Python arguments to functions are passed using a call-by-value model. As I understand it, it is not actually a call-by-value language, but a call-by-object or call-by-sharing model.

按值调用模型和按对象调用模型有什么区别?什么是 Python 中显示这些模型有何不同的示例?

What are the differences between a call-by-value model and a call-by-object model? What is an example in Python that shows how these models are different?

推荐答案

说不是传值是不正确的.在语义上,它传值,并且可以证明它与其他传值语言之间的语义等价.但是,它属于值传递语言的一个特定子类别,其中所有值都是对对象的引用(指针)(您不能将对象直接作为值),许多人将指向对象的指针与对象混淆本身.此类语言包括 Java(关于对象)、Python、Ruby、JavaScript、Scheme、Smalltalk,仅举几例.

Saying that it is not pass-by-value is not correct. Semantically, it is pass-by-value, and one can demonstrate the semantic equivalence between it and other pass-by-value languages. However, it belongs to a particular subcategory of pass-by-value languages where all the values are references (pointers) to objects (you cannot have an object as a value directly), and many people confuse a pointer to an object with the object itself. Such languages include Java (with respect to objects), Python, Ruby, JavaScript, Scheme, Smalltalk, just to name a few.

更令人困惑的是,不同语言的社区使用不同的术语.例如,Java 社区始终将 Java 描述为按值传递,尽管 Java 中的对象与 Python 和 Ruby 中的对象具有完全相同的语义.但是,在后一种语言的社区中,您听到的传值"要少得多.

To add to the confusion is that the communities of the different languages use different terminology. For example, the Java community consistently describes Java as pass-by-value, even though objects in Java have the exact same semantics as objects in Python and Ruby. However, you hear "pass-by-value" much less in these latter languages' communities.

值传递与引用传递本质上是语义上的区别,而不是它的用途之一.但是很多(初学者)程序员并不太关心语义,而更关心它可以用来做什么.例如,有些人认为每当你有我给一个函数的一些数据,它可以改变它而不返回它,我可以看到改变",这意味着传递引用,而没有真正思考(或关心)改变的东西是你传递的东西,还是你传递的东西间接指向的东西.

Pass-by-value vs. pass-by-reference is essentially a semantic distinction, not one of what it is used for. But many (beginner) programmers don't care so much about semantics, and care more about what it can be used to do. For example, some people think that whenever you have "I have some data that I give to a function and it can change it without returning it, and I can see the changes", that means pass-by-reference, without actually thinking (or caring) about whether the thing that was changed was the thing you passed, or something that was indirectly pointed to by the thing you passed.

通过对象传递"或通过共享传递"或任何创造来描述传递值语义组合以及所有值都是引用(指针)的术语.通过这种方式,他们可以更容易地表达这种组合的实际效果,并将其与 C 中按值传递的效果区分开来,例如.但从语义上讲,它仍然是按值传递的.说变量是绑定"到对象的名称"或句柄",并且当您分配或传递它时,您共享对象,完全等效等同于说变量(实际上,语言中的所有值)都是指向对象的指针,当你赋值或传递它们时,这些指针是按值复制的.

"Pass by object" or "pass by sharing" or whatever is a term that coined to describe the combination of pass-by-value semantics along with having all values be references (pointers). In this way, they can more easily express the actual effects of such a combination, and distinguish it from the effects of pass-by-value in C, for example. But semantically it still is pass-by-value. Saying that variables are "names" or "handles" that are "bound" to objects and that when you assign or pass it, you share the object, is exactly equivalent to saying that variables (and in fact, all values in the language) are pointers to objects, and when you assign or pass them, these pointers are copied by value.

这篇关于Python:按值调用和按对象调用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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