OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python: Pass by reference and slice assignment

  • Thread starter Thread starter Diego Puertas
  • Start date Start date
D

Diego Puertas

Guest
In Python, lists are passed by reference to functions, right?

If that is so, what's happening here?

Code:
>>> def f(a):
...     print(a)
...     a = a[:2]
...     print(a)
...
>>> b = [1,2,3]
>>> f(b)
[1, 2, 3]
[1, 2]
>>> print(b)
[1, 2, 3]
>>>
<p>In Python, lists are passed by reference to functions, right?</p>

<p>If that is so, what's happening here?</p>

<pre><code>>>> def f(a):
... print(a)
... a = a[:2]
... print(a)
...
>>> b = [1,2,3]
>>> f(b)
[1, 2, 3]
[1, 2]
>>> print(b)
[1, 2, 3]
>>>
</code></pre>
 
Top