<?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>Dragly &#187; Python</title>
	<atom:link href="http://dragly.org/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://dragly.org</link>
	<description>It was about time I started writing my own tutorials to share some of the knowledge I&#039;ve picked up from around.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:57:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python deleted my vector values</title>
		<link>http://dragly.org/2010/02/15/python-deleted-my-vector-values/</link>
		<comments>http://dragly.org/2010/02/15/python-deleted-my-vector-values/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 16:23:54 +0000</pubDate>
		<dc:creator>Svenn-Arne Dragly</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://svenni.dragly.com/v7/?p=191</guid>
		<description><![CDATA[Sometimes scripting languages can be a real annoyance. Why? Because when you get as much help as you do with for instance Python, you also lose a lot of control. Being used to scripting languages like PHP, I made the &#8230; <a href="http://dragly.org/2010/02/15/python-deleted-my-vector-values/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes scripting languages can be a real annoyance. Why? Because when you get as much help as you do with for instance Python, you also lose a lot of control.</p>
<p>Being used to scripting languages like PHP, I made the funny mistake today of initializing a set of arrays like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = v = r = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This seemed like a really good idea, saving me from typing two extra lines(!). As the sucker I am for short code I was happy with my newfound shortcut. What I didn&#8217;t realize is that Python, in comparison to PHP, treats assignments like these as pointers instead of variables.</p>
<p>I believed this would create three arrays with a lot of zeros in two dimensions as I would expect from PHP, but the result was that I instead created one array with loads of zeros in two dimensions, with three pointers <strong>a</strong>, <strong>v</strong> and <strong>r</strong> all pointing to the same array.</p>
<p>When I then started setting the values for each of these arrays using Euler&#8217;s method, the result was that I got a lot of nonsense in what I thought was three separate arrays.</p>
<p>As a reminder to myself and everyone else out there; Python is not PHP. If you want to initialize three arrays like this in Python, you&#8217;ll have to stick to  the long version:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span>
r = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span>
v = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Or, you could at least save yourself from having to edit each assignment if you ever need to change the code by writing:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span>
r = a.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
v = a.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dragly.org/2010/02/15/python-deleted-my-vector-values/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

