471,092 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Can __init__ not return an object?

When I go to create an object I want to be able to decide whether the
object is valid or not in __init__, and if not, I want the constructor to
return something other than an object, (like maybe None). I seem to be
having problems. At the end of __init__ I say (something like)

if self.something < minvalue:
del self
return None

and it doesn't work. I first tried just the return None, then I got crafty
and tried the del self. Is what I'm trying to do possible in the
constructor or do I have to check after I return? Or would raising an
exception in the constructor be appropriate?

Am I even being clear?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Apr 22 '07 #1
3 9942
On Sat, 21 Apr 2007 22:36:42 -0400, Steven W. Orr wrote:
When I go to create an object I want to be able to decide whether the
object is valid or not in __init__, and if not, I want the constructor to
return something other than an object, (like maybe None).
None is an object, like everything else in Python.

__init__ is not a constructor, it is an initializer -- by the time
__init__ is called, the instance is already constructed.

__init__ is expected to return None.
>>class Foo(object):
.... def __init__(self):
.... return 2
....
>>f = Foo()
__main__:1: RuntimeWarning: __init__() should return None
I seem to be
having problems. At the end of __init__ I say (something like)

if self.something < minvalue:
del self
return None

and it doesn't work.
del self doesn't really do anything useful there, except unbind the name
"self" from the instance.

"return None" is redundant, because all functions and methods will
automatically return None if you don't specify differently.
I first tried just the return None, then I got crafty
and tried the del self. Is what I'm trying to do possible in the
constructor or do I have to check after I return? Or would raising an
exception in the constructor be appropriate?
Yes, absolutely raise an exception.

--
Steven.

Apr 22 '07 #2
On Apr 22, 3:36 am, "Steven W. Orr" <ste...@syslang.netwrote:
When I go to create an object I want to be able to decide whether the
object is valid or not in __init__, and if not, I want the constructor to
return something other than an object, (like maybe None).
[...]

__init__ doesn't create an instance, it initializes it, i.e. does
things like settings some attributes, etc. The method that creates
instances is called __new__ (see http://docs.python.org/ref/customization.html).

--
Arnaud

Apr 22 '07 #3
On Apr 22, 4:36 am, "Steven W. Orr" <ste...@syslang.netwrote:
When I go to create an object I want to be able to decide whether the
object is valid or not in __init__, and if not, I want the constructor to
return something other than an object, (like maybe None). I seem to be
having problems. At the end of __init__ I say (something like)

if self.something < minvalue:
del self
return None

and it doesn't work. I first tried just the return None, then I got crafty
and tried the del self. Is what I'm trying to do possible in the
constructor or do I have to check after I return? Or would raising an
exception in the constructor be appropriate?
You can raise an exception of course but it would just create a side
effect. Another way to achieve what you request for is manipulating
the class creation mechanism.

class A(object):
def __new__(cls, x):
if x == 0:
return None
obj = object.__new__(cls)
obj.__init__(x)
return obj

class B(A):
def __init__(self, x):
self.x = x

The condition can always be checked within the static __new__ method.

Kay

Apr 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by never-aways | last post: by
1 post views Thread by Batista, Facundo | last post: by
9 posts views Thread by Alexander Widera | last post: by
22 posts views Thread by Michael Pradel | last post: by
3 posts views Thread by MathWizard | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.