Why does python not have a mechanism for data hiding?

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Jun 4 05:50:42 CEST 2008


alex23 <wuwei23 at gmail.com> writes:

> So the basic answers I'm seeing that "do just fine" are:
> 
> 1. Don't test private functions.
> 2. Add functionality _to_ the private functions for testing.
> 3. Change the interface for the purpose of testing.
> 
> All of which seem exceptionally inefficient and run counter to the
> whole purpose of unit testing.

It seems you have a different idea of what unit testing is for from
me.

Isn't the entire point of encapsulation to separate internal
components from the external interface?

Why would a unit test, the whole purpose of which is to assert some
aspect of the external behaviour of the unit of code, care about how
that code unit is implemented internally?

If changing the internal, encapsulated components of a unit causes its
external behaviour to change, that's a bug; either in the change made
(it shouldn't have altered the external behaviour), or in the unit
test asserting the wrong thing (it shouldn't be asserting anything
about internal state of the code).

-- 
 \       “Try to become not a man of success, but try rather to become |
  `\                                 a man of value.” —Albert Einstein |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list