Why does python not have a mechanism for data hiding?

NickC ncoghlan at gmail.com
Wed Jun 4 08:09:08 EDT 2008


On Jun 4, 9:56 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> Those unit tests should *not*, though, exercise anything but the
> public API, otherwise they're breaking encapsulation. Their assertion
> should continue to be just as true after a refactoring of the internal
> components as before.

Python must have bad unit tests then - the CPython test suite
explicitly tests private methods all the time.

There's actually an extremely good reason for doing it that way: when
the implementation of an internal method gets broken, the unit tests
flag it explicitly, rather than having to derive the breakage from the
breakage of 'higher level' unit tests (after all, you wouldn't factor
something out into its own method or function if you weren't using it
in at least a couple of different places).

Black box testing (testing only the public API) is certainly
important, but grey box and white box testing that either exploits
knowledge of the implementation when crafting interesting test cases,
or explicitly tests internal APIs can be highly beneficial in
localising faults quickly when something does break (and as any
experienced maintenance programmer will tell you, figuring out what
you actually broke is usually harder than fixing it after you find it).



More information about the Python-list mailing list