[Tutor] Variable Modification in a class

Jeff Shannon jeff@ccvcorp.com
Tue Jun 3 14:22:01 2003


Mehta, Anish wrote:

> I m sorry that i am repeating my last mail. Here also in c i m doing 
> the same thing. Or is there any differnce? 


Yes, there is a difference.  C uses a different philosophy for handling 
variables than Python does, and if you look for strict comparisons 
you'll just get confused.  In C, a variable is a slot in memory.  You 
can change what's in that slot, but the slot stays in the same place. 
 When you say 'c = b' in C, that means "copy the contents of location b 
into location c."  You now have two separate copies of your variable. 
 In Python, a variable is just a name that's bound to an object. 
 There's no space reserved in memory for that variable, so there's 
noplace to copy contents to.  Think of it like a post-it note -- when 
you say 'c = b' in Python, it means "find the object with the post-it 
note that says b, and add another post-it note next to that which says 
c."  Now you can refer to that same object by either b OR c, but it's 
the *same* object either way.  (In many ways, Python variables are 
closer to C pointers, but there's enough semantic differences that 
that's not a very good comparison either.)

Jeff Shannon
Technician/Programmer
Credit International