Python Forum
Passing an argument by reference
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing an argument by reference
#11
I tried out like this
class Spam:
    pass
def fun(a):
    a.foo=5
    print(a.foo)
    print(id(a.foo))

foo=6
id1=5
s=Spam()
fun(s)
print(s.foo)
print(foo)
print(id(foo))
print(id1)
print(id(id1))
And the output is
Output:
5 1918656864 5 6 1918656896 5 1918656864
Interesting.. looks like the same memory reference for value 5 is being shared by both variables a.foo and id1
Reply
#12
Malt Wrote:looks like the same memory reference for value 5 is being shared by both variables a.foo and id1
Yes, python keeps an array of instances for the small integers and reuses them. See here. This is an implementation detail.
Reply
#13
λ python                                                                                          
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information.                            
>>> hash(2)                                                                                       
2                                                                                                 
>>> hash(1)                                                                                       
1                                                                                                 
>>> hash(0)                                                                                       
0                                                                                                 
>>> hash(-1)                                                                                      
-2                                                                                                
>>> hash(-2)                                                                                      
-2                                                                                                
>>> hash(-3)                                                                                      
-3                                                                                                
>>>                                                                                               
If you check values for equality, don't use identity checks (is/is not). Use equality check for integers and comparison for floats.
If you want to check for Types of objects, which exists only once in memory, it's save to use the is operator.

*to check floats of equality, use math.isclose.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing Argument Errors cosmarchy 6 2,430 Sep-29-2021, 06:09 AM
Last Post: snippsat
  Passing argument from top-level function to embedded function JaneTan 2 2,240 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,553 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Mathplotlib - passing reference to axs to function qmfoam 5 2,954 Aug-17-2020, 09:02 PM
Last Post: qmfoam
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,821 Mar-03-2020, 08:34 AM
Last Post: buran
  passing an argument to avoid a global Skaperen 9 3,849 Jul-12-2019, 11:07 PM
Last Post: Skaperen
  Passing by reference or value CanadaGuy 4 2,971 Nov-12-2018, 08:44 PM
Last Post: CanadaGuy
  Passing a list by reference virgik89 6 4,512 Jul-01-2018, 04:26 PM
Last Post: virgik89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020