So immutable objects cannot be modified directly? I guess this means integers are immutable and the act of assigning to one is a completely new definition? So if I were to create a class called Integer and give it a .set() method, this would allow me to create mutable integers, and thus passing in an object of type class Integer would allow me to modify the value from inside the function?
<br><br><div><span class="gmail_quote">On 8/16/07, <b class="gmail_sendername">Steve Holden</b> <<a href="mailto:steve@holdenweb.com">steve@holdenweb.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Robert Dailey wrote:<br>> Hi,<br>><br>> I previously created a topic named "Pass by reference or by value" where<br>> I inquired on how python's function parameters work. I received a lot of<br>> nice responses, however I'm still confused on the topic. Note that I
<br>> come from a C++ background to Python, so any comparisons to C++ would be<br>> very helpful.<br>><br>> I ran a few tests. There's two tests in particular I wanted to show you<br>> guys:<br>> ------------------------------------------------------------------------------------------------
<br>> myvar = []<br>><br>> def changeme( param ):<br>> param.append( "blah" )<br>> print param<br>><br>> changeme( myvar )<br>><br>> print myvar<br>><br>> The above code yields the following output:
<br>> ['blah']<br>> ['blah']<br>><br>> This means that the list passed in was modified by the function.<br>> ------------------------------------------------------------------------------------------------
<br>> Now test case 2:<br>><br>> myvar = 4<br>><br>> def changeme( param ):<br>> param = 5<br>> print param<br>><br>> changeme( myvar )<br>><br>> print myvar<br>><br>> The above code yields the following output:
<br>> 5<br>> 4<br>><br>> This means that the integer passed in was NOT modified by the function.<br>> ------------------------------------------------------------------------------------------------<br>>
<br>> Between these two tests, both types passed in are mutable objects. I'm<br>> having trouble figuring out what mandates an object to be changed from<br>> within a function versus not. What is happening in test case 2 to cause
<br>> it to not be modified?<br>><br>> Thanks for reading guys. Hopefully one day I'll understand this lol.<br>><br>The first thin to realise is that all Python names are just bindings to<br>values. In C++ terms you can think of them all as pointers.
<br>De-referencing is automatic when a value is to be retrieved.<br><br><br> >>> def changeme( param ):<br>... param = 3<br>... print param<br>...<br> >>> myvar = []<br> >>> changeme(myvar)
<br>3<br> >>> myvar<br>[]<br> >>><br><br>In this case there is no attempt to mutate the argument, the argument<br>name is simply bound to another value. Since the argument is a name<br>local to the function, this does not result in any change outside the
<br>function.<br><br>In this case the argument is bound to a mutable value, so the call to<br>append mutates the object (a list) referenced by the argument.<br><br>Unlike C++ and similar languages a variable does not hold a value, it
<br>holds a pointer to a value. When a list is passed as a function argument<br>the reference to the list is copied into the argument. Does this help at<br>all?<br><br>regards<br> Steve<br>--<br>Steve Holden +1 571 484 6266 +1 800 494 3119
<br>Holden Web LLC/Ltd <a href="http://www.holdenweb.com">http://www.holdenweb.com</a><br>Skype: holdenweb <a href="http://del.icio.us/steve.holden">http://del.icio.us/steve.holden</a><br>--------------- Asciimercial ------------------
<br>Get on the web: Blog, lens and tag the Internet<br>Many services currently offer free registration<br>----------- Thank You for Reading -------------<br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">
http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>