Returning 'None' on Instantiation ?
Emile van Sebille
emile at fenx.com
Fri Jun 1 10:28:30 EDT 2001
The Object self is created before __init__ populates it and survives
__init__. You could use __nonzero__ to meet your example's needs:
>>> class myClass:
def __init__(self, arg):
self.isValid = 1
if arg < 5:
self.isValid = 0
def __nonzero__(self):
return self.isValid
>>> a = myClass(3)
>>> if a: print "OK"
>>> b = myClass(8)
>>> if b: print "OK"
OK
>>>
HTH,
Emile van Sebille
emile at fenx.com
---------
----- Original Message -----
From: "Dave Haynes" <Dave.Haynes at sia-mce.co.uk>
Newsgroups: comp.lang.python
Sent: Friday, June 01, 2001 6:28 AM
Subject: Returning 'None' on Instantiation ?
> Is there a way to optionally return 'None' on instantiation? I'm
> thinking of something like the following:
>
> class myClass:
> def __init__(self, param):
>
> if <check param>:
> <initialise data members>
> else:
> <make self a 'None object'>
>
>
> myParam = GetParam()
> myObj = myClass(myParam)
>
> if myObj:
> print 'Okay'
> else:
> print 'Couldn't make myObj from myParam'
>
> This is a fairly common state of affairs. If there's nothing specific
> in Python to accomodate this, what methods are considered robust by
> this NG?
>
>
> Best Regards,
>
>
> Dave.
More information about the Python-list
mailing list