在 Python 中传递值 [英] Passing values in Python

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

问题描述

当你将一个像列表、数组这样的集合传递给 python 中的另一个函数时,它是复制它还是只是一个指针?

When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?

推荐答案

Python 按值传递对对象的引用.

Python 通过以下方式传递对对象的引用值(如 Java),以及其中的一切Python 是一个对象.这听起来很简单,但是你会注意到某些数据类型似乎表现出传值特征,而其他人似乎表现得像传递引用……这是怎么回事?

Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?

理解可变的很重要和不可变对象.一些对象,像字符串、元组和数字一样,不可变的.在一个内部改变它们函数/方法将创建一个新的实例和原始实例函数/方法之外不是改变了.其他对象,如列表和字典是可变的,这意味着您可以更改对象到位.因此,改变一个函数/方法内的对象将也改变原来的对象外面.

It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

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

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