gpt4 book ai didi

python - 在 Python 中传递值

转载 作者:IT老高 更新时间:2023-10-28 20:22:31 28 4
gpt4 key购买 nike

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

最佳答案

Python passes references-to-objects by value .

Python passes references-to-objects byvalue (like Java), and everything inPython is an object. This soundssimple, but then you will notice thatsome data types seem to exhibitpass-by-value characteristics, whileothers seem to act likepass-by-reference... what's the deal?

It is important to understand mutableand immutable objects. Some objects,like strings, tuples, and numbers, areimmutable. Altering them inside afunction/method will create a newinstance and the original instanceoutside the function/method is notchanged. Other objects, like listsand dictionaries are mutable, whichmeans you can change the objectin-place. Therefore, altering anobject inside a function/method willalso change the original objectoutside.

关于python - 在 Python 中传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/534375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com