Hi, I previously created a topic named "Pass by reference or by value" where I inquired on how python's function parameters work. I received a lot of nice responses, however I'm still confused on the topic. Note that I come from a C++ background to Python, so any comparisons to C++ would be very helpful.
I ran a few tests. There's two tests in particular I wanted to show you guys: ------------------------------------------------------------------------------------------------ myvar = [] def changeme( param ): param.append( "blah" ) print param changeme( myvar ) print myvar The above code yields the following output: ['blah'] ['blah'] This means that the list passed in was modified by the function. ------------------------------------------------------------------------------------------------ Now test case 2: myvar = 4 def changeme( param ): param = 5 print param changeme( myvar ) print myvar The above code yields the following output: 5 4 This means that the integer passed in was NOT modified by the function. ------------------------------------------------------------------------------------------------ Between these two tests, both types passed in are mutable objects. I'm having trouble figuring out what mandates an object to be changed from within a function versus not. What is happening in test case 2 to cause it to not be modified? Thanks for reading guys. Hopefully one day I'll understand this lol.
-- http://mail.python.org/mailman/listinfo/python-list