[Tutor] From C to Python

Kent Johnson kent37 at tds.net
Thu Jul 5 14:07:24 CEST 2007

Dhruva Kulkarni wrote:
> Hi,
>     I am a C guy wanting to switch to Python for some 
> application-type-tasks that might be done in Python much quicker and 
> sooner and smaller than in C...I have fiddled around with Python and 
> looked through the tutorial, but i'm a bit lost when there are no 
> pointers ( pun intended! ). I could not find any material on 
> 'what-happens-when' type questions, e.g.( how functions are implemented 
> --> stack-frame struct , parameter passing by value / reference ? ),  
> and  the whole 'identity vs equality' thing, plus the 
> dynamic-typing-of-objects...
> So i can see that Python programers must be thinking in a very 
> un-'byte'-ly way, so do I need to unlearn the C way of wandering in 
> 'byteland' ? And where can i find the ten commandments for Pythonland>? 

Yes, you need to leave your C ways behind. Don't worry, you'll like it 
here :-)

The biggest shift is in the way you think about variables. In C, a 
variable is a container for a value. In Python, a variable is a name for 
a value; a reference. In C terms, think of it as a pointer to a value.

This might help, especially the introduction :-)

There is a lot of dispute over what to call the semantics of argument 
passing (pass by value? pass by reference?), but the concept is pretty 
simple. Function arguments are references that are bound to names in the 
local scope of the function. The reference is passed by value. One name 
for this is "call by object reference". In C terms, if you think of 
variables as pointers to values, and argument passing as copying the 
pointer, you will get the idea.

BTW the semantics of assignment and those of argument passing are 
identical - they both bind a name to a value in the local scope.

One consequence of this is that aliasing happens all the time. If two 
names are bound to the same mutable object, any changes to the object 
are visible through either name:
In [11]: a=[1, 2, 3]
In [12]: b=a
In [13]: a.append(4)
In [14]: a
Out[14]: [1, 2, 3, 4]
In [15]: b
Out[15]: [1, 2, 3, 4]

You really don't have to worry about stack frames and how functions are 
implemented (they are objects too), at least not initially, just let 
Python take care of it for you. I wouldn't worry about identity vs 
equality, either; usually you are interested in equality.

I don't know about ten commandments, but you can see the Zen of Python 
by typing 'import this' at the Python interpreter prompt.


More information about the Tutor mailing list