Why less emphasis on private data?

Diez B. Roggisch deets at nospam.web.de
Sat Jan 6 19:41:44 EST 2007


time.swift at gmail.com schrieb:
> Coming from a C++ / C# background, the lack of emphasis on private data
> seems weird to me. I've often found wrapping private data useful to
> prevent bugs and enforce error checking..
> 
> It appears to me (perhaps wrongly) that Python prefers to leave class
> data public.  What is the logic behind that choice?

Private data is a convention, not a strict enforcement, for both Java 
and C++.

Depending on your C++ compiler, a simple

#define private public

will give you access to all data you want. Besides the fact that casting 
to a void* pointer and just accessing the private parts isn't rocket 
science.

The same applies to java, for whatever reasons (I presume 
serialization), you can access private fields via reflection.

In python, private members are usually declared using a single or double 
underscore. And the basic idea is: "if you tamper with this, you've been 
warned". Which is the way coding between consenting adults should be.

To be honest: I've stumbled over more cases of unescessary hoops to jump 
through due to private declarations than bugs caused of me exploiting 
things I've been told by the compiler not to tamper with it.

Summary: not important, forget about it, enjoy python!

Diez



More information about the Python-list mailing list