Preventing direct access to a class

Jay O'Connor joconnor at cybermesa.com
Sun Feb 4 15:15:26 EST 2001


Daniel Klein wrote:
> 
> I have a situation where I need to prevent direct access to a class. IOW, the
> only way I wish a class to be instantiated is via another class.

Any reason you wish to enforce this particular restriction?  Usually
such notions are really means of communicating with other developers the
proper use and intent of the code you've written and I've found that
commenting with something akin to  "This is a private class, do not
instantiate directly" (but with more information) is sufficient to
communicate to other developers that they shouldn't use the class like
that.


So if you have a class that shoudn't be instantiated except from another
class, it should be sufficient to comment it as such.  And if someone
does it anyway, they get to deal with the ramifications :)

I think code is as much communicating intent to other developers as much
as communicating instructions to the computer and I like that Python
doesn't have a lot of syntax (or mechanisms) to enforce decisions that
are really communications issues that can be handled without extra
language features

Take care,

-- 
Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor

"God himself plays the bass strings first when He tunes the soul"



More information about the Python-list mailing list