can a class intantiate itself to None?

David Allen mda at
Sat Mar 17 19:38:56 CET 2001

In article <mailman.984770427.21248.python-list at>, "Timothy Grant"
<tjg at> wrote:

> I'd like to create a class that can instantiate itself to None when
> instantiation fails...
> class foo:
> 	def __init__(self, bar):
> 		if type(bar) == StringType:
> = 'This is a String'
> x = foo('a')
> y = foo(1)  # In this instance I would like y to be None
> So, can it be done, or do I need to raise an exception, catch it and then
> set y to None?

Raise an exception and set y to None.

You have the option of setting all class data to 
None if something doesn't happen that you want to 
happen, but there isn't a way that you can actually
return None from a constructor as far as I know of.
Besides, that would be confusing as hell, and that's
partially what exceptions are for, so use 'em.

David Allen
Be wary of strong drink. It can make you shoot at tax collectors and 
- Lazarus Long, "Time Enough for Love"

More information about the Python-list mailing list