<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; variable</title> <atom:link href="https://bogdan.org.ua/tags/variable/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>Python: passing by value vs passing by reference</title><link>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html</link> <comments>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html#comments</comments> <pubDate>Mon, 11 Feb 2008 16:06:22 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[by reference]]></category> <category><![CDATA[by value]]></category> <category><![CDATA[function]]></category> <category><![CDATA[object]]></category> <category><![CDATA[scope]]></category> <category><![CDATA[variable]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html</guid> <description><![CDATA[Note: this a collection of scraps, describing how values (de)referencing works in Python, and describing when your variable is either a value or a reference. Primary source of knowledge for this post (also here). Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will [&#8230;]]]></description> <content:encoded><![CDATA[<p>Note: this a collection of scraps, describing how values (de)referencing works in Python, and describing when your variable is either a value or a reference. <a
href="http://www.testingreflections.com/node/view/5126" class="broken_link" rel="nofollow">Primary source of knowledge</a> for this post (also <a
href="http://bytes.com/topic/python/answers/37219-value-reference">here</a>).</p><p>Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference&#8230; what&#8217;s the deal?</p><p>It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable.  Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed.  Other objects, like lists and dictionaries are mutable, which means you can change the object in-place.  Therefore, altering an object inside a function/method will also change the original object outside.</p><p>Immutable variables &#8211; such as integers [<a
href="http://bytes.com/topic/python/answers/37219-value-reference">strings, numerics and tuples are immutables</a>] &#8211; are passed by value. That is, if your function accepts some integer argument, you are safe assuming that your function won&#8217;t be able to modify your integer. Mutable variables &#8211; such as dictionaries and lists &#8211; are passed by reference, and so if your function accepts mutable argument, it may modify the contents of that mutable variable outside the scope of the function.</p><p>When doing :<br
/> s = &#8220;Hello &#8221;<br
/> s += &#8220;World&#8221;<br
/> &#8230; you are not modifying the string object bound to s, but creating a new string object and binding it to s.</p><p>If using object&#8217;s methods within a called function, variable is considered &#8220;passed by reference&#8221; &#8211; it is modified out of the function&#8217;s scope. If using assignment on a mutable object, it is created a-new within the function, and global value isn&#8217;t modified.</p><p>When you call a function with an arg, a &#8220;local variable&#8221; is created, which references the object passed as the argument. (well&#8230; an entry with the formal parameter name as key and a reference to the object passed in is created in the &#8216;local&#8217; dict).</p><p>So, rebinding this local symbol does not impact the binding in the caller&#8217;s namespace &#8211; because the symbol lives in another namespace.</p><p>*But* &#8211; and if the object referenced is mutable of course &#8211; modifying the object in the function&#8230; well, just modifies the object, because it&#8217;s the *same* object that is bound to (&#8216;referenced by&#8217;, if you prefer) both symbols (the one in the caller&#8217;s namespace and the one in the function&#8217;s namespace). So yes, the object *is* modified when the function returns.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&#038;title=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" data-a2a-url="https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html" data-a2a-title="Python: passing by value vs passing by reference"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> </channel> </rss>