is python Object oriented??
thmpsn.m.k at gmail.com
thmpsn.m.k at gmail.com
Sun Feb 1 00:28:14 CET 2009
On Jan 31, 2:27 pm, Christian Heimes <li... at cheimes.de> wrote:
> thmpsn.... at gmail.com schrieb:
> > But it's only a faking, and things such as inheritance and
> > polymorphism are implemented clumsily (actually I'm not even sure
> > about polymorphism). And of course, there are still no private
> > members.
> Do you honestly believe that C++'s private members are really private?
> Privateness is only enforced during parsing time. Nobody can stop you
> from messing around with header files or memory. You can still access
> and modify private members but it's all messy and ugly. Even C# and .NET
> don't stop you from doing nasty things from unmananged assemblies.
I don't know how you would do it in C# (or Java for that matter).
In C++ you can play with pointers to "get at" some memory location
somewhere in the object. The only portable way to know the exact
location between the beginning of the object and the desired member is
the offsetof() macro, but as I understand it this only works for POD
types, which means that it won't work for classes such as:
(I haven't ever actually tried it, so I'm not sure.)
Nevertheless, you can play and hope for the best. For example, if the
member you want to get at is 'b', then you can do:
std::cout << "obj.b = " << *(int*) ((unsigned char*) &obj + sizeof
(int)) << std::endl;
and hope that the compiler didn't leave a hole between the 'a' member
and the 'b' member.
Getting at the 'a' member would be easier because the first member of
a struct/class always has the same memory location as the object
itself (although again I'm not sure if this is true for non-POD types
So: Sometimes it may work, usually it will be unsafe and/or non-
portable, and in most cases the procedure will look complicated. It
certainly isn't something I'd try in a real application. However, it
WOULD be tempting to access the member if the language allows me to
print "obj.b =", obj.b
and be done with it.
Personally, in Python, I use double underscores for "private" members,
so the above would look like:
print "obj.b =", obj._NonPOD__b
but it's still easier than the C++ example.
> Seriously, 'private' and 'protected' are merely tools to stop bad
> programmers from doing bad stuff. And the serve as documentation, too.
It's not just documentation. For example, suppose you're reading a
class definition and see the declaration of a veryInterestingMember
and forget that you're not supposed to access it. If you try to access
it, the compiler will give you a diagnostic message at compile time.
So you can think of it as an error-checking tool as well.
> Oh, by the way, the first C++ compilers just converted C++ code to C
> code. Such much about "You can't do OOP in C."!
More interestingly, though, most compilers translate C and C++ code to
assembler first. Does that mean that you can do object-oriented
programming, generic programming, and procedural programming in
(Answer: No, but you can probably -- very clumsily -- fake them.)
More information about the Python-list