Python. Parameters and returned values

雨燕双飞 提交于 2019-12-01 16:42:07

Your mental model is pretty much correct. However, you should not worry about whether Python is "pass by value" or "pass by reference". You have to learn how assignment in Python works. This is hands down the best resource to do so. The most important fact to know is that assignment never copies data.

Once you understand assignment, the only thing you have to know is that function parameters are passed by assignment.

The first thing that happens implicitly when you call your function is

NN = unsort_letters

You passed in unsort_letters as the argument. You assign another name (NN) to that argument - and that's it, no data is copied.

Is there ever a circumstance when I cannot change the contents of the passed parameter within the function

Yes, when whatever you pass in is immutable. Integers, for example, have no methods to update their value, so you can't mutate them in the function body (or anywhere else).

It is important to note however that Python does not treat mutable and immutable types differently during assignment and parameter passing. You simply cannot mutate immutable types because they have no mutating methods on them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!