[Tutor] Objects C++ vs Python
Alan Gauld
alan.gauld at btinternet.com
Thu Jun 9 10:48:44 CEST 2011
"Ashwini Oruganti" <msg.ashwini at gmail.com> 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.
No, although its a common misconception.
An object in OOP, in any language is the encapsulation of data
and the operations that act on that data. It is first and foremost
a conceptuial entity. It just so happens that the normal way of
producing objects in C++ is by instantiating classes.
[But you can create objects in C++ without (explicitly) instantiating
a class, for example when passing arguments to functions/methods
temporary objects are frequently created. Also when using
templated classes there is no explicit class for a List<int> or a
List<float>
we are using an implied class produced by the template at
compile/runtime.]
> int x; // x is NOT an object, it is a *variable*
C++ grew out of C so it has a lot of non OOP features. It is no
surprise to find therefore that its primitive types are related to
memory allocation and raw data rather than objects.
> 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.
There is an object created (the integer 5) that is associated with
a name(x), yes. But forget about memory allocation because,
depending on implementation, no new memory may be required.
> Further, in python, *everything *is an object, while in C++,
> only*instances of a class * are called objects.
Yes, because thats how objects are created in C++, its one
of the limitations of C++ as a hybrid OOP language.
> So does the term *Object * change its meaning when we
> shift the context from C++ to python?? This is a little confusing,
No object is standard in OOP. It is a concept. It is the instantiated
encapsulation of data and function. How it is created varies between
language implementations.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list