4 ms·
This is indeed correct. Object references themselves in Java/Python are passed by value, which is why the following two code samples do not have the same effect
by rockemsockem 4y ago
This is indeed correct. Object references themselves in Java/Python are passed by value, which is why the following two code samples do not have the same effect:
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(d):
... d['foo'] 'bar'}
...
>>> a = {}
>>> f(a)
>>> a
{'foo': 'bar'}
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(d):
... d = {'foo': 'bar'}
...
>>> a = {}
>>> f(a)
>>> a
{}
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]