[Tutor] Objects C++ vs Python

Steve Willoughby steve at alchemy.com
Thu Jun 9 08:01:53 CEST 2011


On 08-Jun-11 22:38, Ashwini Oruganti wrote:
> I'm trying to learn Python, and know C++. I have a slight confusion
> regarding the meaning of "object" in python. Here's what I've concluded
> so far:
>
> When we say "object" in C++, it means an instance of a class.
> e.g.

This is true in both Python and C++.

>     int x;                // x is NOT  an object, it is a *variable*

You're confusing variables and the things they hold.  x here is a 
variable which contains an "int" type value.  Another variable might
hold a pointer to an object.

> while in python, from what i've understood so far,
>  >>> x=5
> implies that there's a memory allocation (called object) that holds the
> value 3, and "x" is the variable (or name) that is used to refer to it.

Maybe this will be more clear:

The value 5 is an integer-class object.  Full stop.  Don't even go down 
the road of thinking of "memory allocation" (yet).  It's an object 
floating around in Python's runtime somewhere.

x is a name you gave to that object for now, so you can refer to it 
somehow.

The big difference is that variable names in Python are really just 
names for objects (something like pointers/references in C++ but a lot 
easier to work with), while in C++ they refer to specific memory 
locations, which must be the right size for what you store into them. 
Since Python is just naming objects, there is no such problem.

That has nothing to do with what an "object" means.

> So does the term *Object * change its meaning when we shift the context
> from C++ to python?? This is a little confusing, can someone clear it up??

Not really.  I think your confusion was about variables.

-- 
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C


More information about the Tutor mailing list