[Tutor] A class that instantiates conditionally ?

Alan Gauld alan.gauld at btinternet.com
Tue Mar 1 17:35:54 CET 2011


"David" <bouncingcats at gmail.com> wrote

> clever enough to refuse to instantiate itself if a necessary 
> condition
> is not met.

Oops, sent too soon.

I meant to add that you should realize that the implication of your
design is that the user of the class now has to check each object
to see if it is a valid reference or None. You could raise an 
exception
instead of returning None which allows a try/except style...

This extra overhead is one reason these kinds of "clever" tricks
are usually avoided. A valid object with null content is often
preferrable, or a singleton style pattern. But occasionally your
style is needed, just be aware of the extra overhead you
introduce by using it.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list