All Python arguments are passed by reference

It seems. I didn't know. Or rather, I didn't really care.

I thought that int was treated specially and passed by value from the viewpoint of behavior, but it was passed by reference for "all".

Below is an experiment with ipython.

In [27]: i = 1

In [28]: id(i)
Out[28]: 140513240953112

In [29]: def func(a):
   ....:     print a, id(a)
   ....:     a += 1
   ....:     print a, id(a)
   ....:     return a
   ....: 

In [30]: b = func(i)
1 140513240953112	<-Same place so far(id)Referencing
2 140513240953088 <-Another place here(id)Copied to

In [31]: id(b)
Out[31]: 140513240953088

Another example

In [32]: i = 1

In [33]: id(i)
Out[33]: 140513240953112

In [34]: def func(a):
   ....:     print a, id(a)
   ....:     return a
   ....: 

In [35]: b = func(i)
1 140513240953112

In [36]: id(b)
Out[36]: 140513240953112	<-Again the same place as i(id)Referencing

In [37]: b
Out[37]: 1	<-Reading from the same location as i

In [38]: b += 2 <-Another place here(id)Copied to

In [39]: id(b)
Out[39]: 140513240953064 <-You see, it's strange

Hey. That's what it is.

reference

-[Python] Common sense, right? An introduction to arguments to avoid being told

Impressions

When I tried hard to tune with python, the reference cost via the Array index became a bottleneck until the end. Well, if you are familiar with python, you may still be able to do it, but it is faster to change the language than trying hard with python, so it is necessary to dive into the deep part of python. There is no. I'm interested.

As an option to change the language, I easily go to C / C ++ now, but I think it is necessary to make the Go language more responsive. I'm thinking. really.

end.

Recommended Posts

All Python arguments are passed by reference
Python> these are all considered False:
Python> dictionary> values ()> Get All Values by Using values ()
About python reference
Python reference page
Execute Power Query by passing arguments to Python
What are python tuples and * args after all?
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Python default arguments are not initialized on every call
Primality test by Python
About function arguments (python)
Visualization memo by Python
Communication processing by Python
Python classes are slow
Python: About function arguments
Passing by value, passing by reference, passing by reference,
Beamformer response by python
Summary of Python arguments
About __all__ in python
Make sure all the elements in the list are the same in Python
The twelfth offline real-time writing reference problem. Implementation by python