Re: is python Object oriented??



On Jan 31, 2:27 pm, Christian Heimes <li...@xxxxxxxxxx> wrote:
thmpsn....@xxxxxxxxx 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:

class NonPOD
{
private:
int a;
int b;
public:
NonPOD();
~NonPOD();
int C();
};

(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:

NonPOD obj;
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
as well).

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
just write:

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
assembler?

(Answer: No, but you can probably -- very clumsily -- fake them.)


.



Relevant Pages

  • Re: How do I write to a textbox in a form from within a class
    ... you can control the level of access for each class member. ... Protected Friend ... Private PrivateVar As Integer ... to your TextBox issue ... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: "this" and "base"
    ... compiler and the programming language. ... a private inherited field in a base class from a derived class is that the ... > because the member "is a private". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: "this" and "base"
    ... "What keeps you from accessing a Private member" is more of what you are ... It is both the Compiler and Runtime engine that keep you from ... member function in your derived class you could have a local variable called ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does python not have a mechanism for data hiding?
    ... define a new class member from completely outside the class ... that cannot be declared private. ... would need to use a "mangled" name to access private data or methods. ... without source code? ...
    (comp.lang.python)
  • Re: Ok Topper if you want to talk about "Troll Post" Addition
    ... Go ahead and waste time trying to find one instance where I posted PRIVATE, personal information about you... ... Knowingly spreading false rumors about another community member. ... Look at all the people that have asked you to stop posting to them, or sending email to them, and yet you persist. ...
    (misc.transport.trucking)