[Tutor] A class that instantiates conditionally ?
hugo.yoshi at gmail.com
Tue Mar 1 18:06:28 CET 2011
On Tue, Mar 1, 2011 at 5:35 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "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.
Side question: Any reason why you'd raise the exception from __new__
rather than __init__? If you want to return None, then yeah I can see
why you'd have to use __new__, but would there be any reason you can't
or shouldn't simply raise an exception from __init__?
More information about the Tutor