is python Object oriented??

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Feb 1 02:50:38 EST 2009


On Sat, 31 Jan 2009 15:28:14 -0800, thmpsn.m.k wrote:

> On Jan 31, 2:27 pm, Christian Heimes <li... at cheimes.de> wrote:
>> 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 Java you can use reflection to get your fingers on private fields.

> In C++ you can play with pointers to "get at" some memory location
> somewhere in the object.
> [Snipped pointer fiddling explanation]
> So: Sometimes it may work, usually it will be unsafe and/or non-
> portable, and in most cases the procedure will look complicated.

Or ``#define private public`` before including the header files.  Which 
doesn't look complicated to me.

> 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.

And that's perfectly okay, because the author would have prepended an 
underscore to `b` if you should not mess 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.

The name mangling is there to prevent name clashes, not to make 
attributes inaccessible.  It's useful for mixin classes for 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.

I can't forget that because if I'm not supposed to access it, its name 
would be `_very_interesting_member`.  I don't have to wait till compile 
time to see that, it is really obvious at the time I write the code to 
access it, or decide not to because it is not part of the API.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list