Why does python not have a mechanism for data hiding?

Roy Smith roy at panix.com
Wed Jun 4 14:54:06 CEST 2008


In article <87y75llp5x.fsf at benfinney.id.au>,
 Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:

> By definition, "private" functions are not part of the publicly
> documented behaviour of the unit. Any behaviour exhibited by some
> private component is seen externally as a behaviour of some public
> component.

You know the difference between theory and reality?  In theory, there is 
none...  Sometimes it's useful to test internal components.  Imagine this 
class:

class ArmegeddonMachine:
   def pushTheButton(self):
      "Destroy a random city"
      city = self._pickCity()
      self._destroy(city)

   def _pickCity():
      cities = ['New York', 'Moscow', 'Tokyo', 'Beijing', 'Mumbai']
      thePoorSchmucks = random.choice(cities)
      return 'New York'

   def _destroy(self, city):
      missle = ICBM()
      missle.aim(city)
      missle.launch()

The only externally visible interface is pushTheButton(), yet you don't 
really want to call that during testing.  What you do want to do is test 
that a random city really does get picked.

You can do one of two things at this point.  You can say, "But, that's not 
part of the externally visible interface" and refuse to test it, or you can 
figure out a way to test it.  Up to you.



More information about the Python-list mailing list