Class returning None ?

G

George Marshall

Hi, I'm wondering, what's the best way to check for
success or failure on object initialization ?

This is my ugly workaround to do that :

class mytest:
status=0
def __init__ (self):
status=1

_mytest = mytest()

def mytest ():
if everythingok():
return _mytest()
else:
return None

def everythingok():
return False

def main():
a=mytest()
if a==None:
return 1

if __name__ == "__main__"
main()


What's the python way to do that ?

Thanks.
 
C

Cliff Wells

Hi, I'm wondering, what's the best way to check for
success or failure on object initialization ?

Raise an exception.

This is my ugly workaround to do that :
[snip]

What's the python way to do that ?

from exceptions import Exception

class SomeError(Exception): pass

class mytest:
def __init__(self):
if not everythingok():
raise SomeError

def main():
try:
a = mytest()
except SomeError:
return 1

Regards,
Cliff
 
A

Aahz

from exceptions import Exception
class SomeError(Exception): pass

BTW, there's no need to do this. ``Exception`` lives in the built-in
namespace.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
 
C

Cliff Wells

BTW, there's no need to do this. ``Exception`` lives in the built-in
namespace.

Er, explicit is better than implicit? ;-)

Somehow I never noticed that.

Regards,
Cliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,501
Latest member
log5Sshell/alfa5

Latest Threads

Top