Skip to main content

Crazy Python pointer

It took me ~1 hour to find a silly bug because of the "call-by-value" thing in python:


>> a = b = {}
>> a[0] = 1
>> print a
{0:1}
>>b[0] = 9
>> print a
{0:9}
>> print id(a)
4297396640
>> print id(b)
4297396640


When you change the value of a copied variable, the formal one changes, too.

So yes, as it is confusing usually how parameters are passed in different programming languages. What happens when a variable is passed and whether its value will be changed or not.

In python, a variable actually holds a reference (pointer, address) of an object. When the variable is passed as a parameter, its value is copied into the formal parameter variable.

>>x = 1
>>print id(x)
39030144
>>def f(x):
>> print id(x)
39030144
>> x = 2
>> print id(x)
39030132


>>f(x)
>>print x
1

Comments

Popular posts from this blog

Random variables

A random variable is a mapping from a sample space to real numbers $\Omega \rightarrow \mathrm{R}$ At a certain point in most probability courses, we don't see the sample space, but it's always there, lurking in the background. For example: Let $\Omega = \{(x,y); x^2 + y^2 \leq 1\}$ be the unit disc. Consider drawing a point "at random" from $\Omega$. Outcome: $\omega = (x,y)$. Examples of random variables: $X(\omega) = x$, $X(\omega) = y$, $Z(\omega) = x + y$

Quick text files merging, data preparation

It's very often that in natural language processing, you will have to re-format your data to take as inputs to different systems. In this case, these simple linux commands will help you do it much quicker without having to write a script. 1. Merging two files to one file with two column Input f1 looks like this: 1 2 3 4 Input f2 looks like this: a b c d Output f3 will look like this: 1  a 2  b 3  c 4  d Command: paste f1 f2 > f3  The delimiter by default is a tab. You can also define it (for example, separated by a comma) as follows: paste -d ',' f1 f2 > f3 2.  Create a line number to each line of a text file Assume that you want to create an index to each line in a text file, i.e. inserting a line number and then a tab before the content of each line: Input f1: a b c d Output f2: 1  a 2  b 3  c 4  d Command: nl f1 > f2 3. Joining two files with a common field Input f1: 1   aaa...