object.enable() anti-pattern

Roy Smith roy at panix.com
Fri May 10 11:21:33 EDT 2013


In article <mailman.1532.1368198547.3114.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> > Each language has its own set of best practices.  Trying to write C++
> > code using Python patterns is as bad as trying to write Python code
> > using C++ patterns.
> 
> Agreed, in generality. But what is actually gained by hiding data from
> yourself? 

You're not hiding it from yourself.  You're hiding it from the other 
people who are using your code and may not understand all the subtle 
gotchas as well as you do.

In the calling-virtual-methods-in-the-constructor case we're talking 
about here, constructing an object by calling B() without immediately 
following it up with a call to B::PostInit() is dangerous.  If you 
document that you shouldn't do that, but allow it anyway, people will do 
it.  Better to disallow it completely.  People will bitch when their 
code doesn't compile, but that's better than compiling and running and 
producing the wrong results.

You solve this problem in Python by simply having the constructor do the 
right thing.



More information about the Python-list mailing list