的Python:什么是呼叫按值和Call-按对象之间的区别? [英] Python: What is the difference between Call-by-Value and Call-by-Object?

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

问题描述

很多人说,在Python函数的参数使用的是呼叫按值传递模型。据我了解,它实际上不是一个呼叫按值语言,但是呼叫按对象或呼叫通过共享模式。

什么是呼叫按值模型和的call-by-对象模型之间的区别?什么是Python中,显示这些模型是如何不同的例子吗?


解决方案

说,这是无法通过按值是不正确的。语义上,它的的传递由值,和一个可以证明它和其他通行证按值语言之间的语义等价。然而,它属于通过噪声值的语言,所有的值都引用(指针)对象的特定子类别(你不能有一个对象作为直接的值),很多人混淆了一个指向对象与对象本身。这样的语言包括Java的(相对于对象),Python和Ruby,JavaScript中,计划,Smalltalk中,只是仅举几例。

要加入到混乱的是,不同语言的社区使用不同的术语。例如,Java社区一直描述Java作为传递按值,即使在Java对象拥有的完全一样的的语义在Python和Ruby对象。然而,听到传递按值更在后者语言的社区。

传递按值对通过按引用本质上是一个语义差别,而不是它用于一个。但是,许多(初级)程序员不那么在乎的语义,更关心的是什么,可以用来做。例如,有些人认为,只要你有我有一些数据,我给一个功能,它可以改变它不还,我可以看到的变化,这意味着通过按参考,实际上并没有思考(或关怀)关于变更的东西是否是被间接地传递你的东西指着你过去了,什么事情。

的对象传递或或任何通过分享传递,是用来形容具有的所有值是引用(指针)一起传递值语义相结合的术语。以这种方式,它们可以更容易地恩preSS这样的组合的实际效果,并从的影响区分开来传递由值在C为例。但语义上它仍然是通过按值。话说变量是名称或手柄是绑定对象和当您分配或通过它,你分享的对象,是的完全等同的在说,变量(实际上在语言的所有值)对象的指针,当你分配或通过他们,这些指针是按值复制。

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.

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?

解决方案

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.

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.

"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:什么是呼叫按值和Call-按对象之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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