473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Class returning None ?

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.

Jul 18 '05 #1
3 1842
On Mon, 2004-01-26 at 00:43, George Marshall wrote:
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

--
Pushing the stone up the hill of failure
-Swans
Jul 18 '05 #2
In article <ma**************************************@python.o rg>,
Cliff Wells <cl************@comcast.net> wrote:

from exceptions import Exception
class SomeError(Exception): pass


BTW, there's no need to do this. ``Exception`` lives in the built-in
namespace.
--
Aahz (aa**@pythoncraft.com) <*> 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
Jul 18 '05 #3
On Mon, 2004-01-26 at 06:33, Aahz wrote:
In article <ma**************************************@python.o rg>,
Cliff Wells <cl************@comcast.net> wrote:

from exceptions import Exception
class SomeError(Exception): pass


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

--
So sad, love lies there still
-Bauhaus
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Qp | last post by:
Hello. I'm building a simple chat server and client interface, and I've got everything working except for this: While the client's basic.LineReceiver protocol class can sendLine when a...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
10
by: Sachin Garg | last post by:
Hi, When trying to return objects of type std::list<MyClass> from my function, I get a corrupt heap. (I checked using MSVC++ 7.0 runtime heap check facility) Basically, I am creating a...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
10
by: randomtalk | last post by:
hello, i have another problem i feel that i have to be missing something.. Basically, i've written a recursive function to find all the prime up to a number (lim).. here is the function: The...
8
by: sam | last post by:
i am starting to experiment with recursion, and decided to write a fairly trivial little program which took a float as input, then called a function to halve it recursively until it was less than...
16
by: BartlebyScrivener | last post by:
I am a mere hobbyist. Spent several hours trying to make a class, because I think this is an occasion where I need one. But I can't make it work. This code "works" (only because of the global c,...
13
by: agent-s | last post by:
I have a function, generally described as so: def function(args): if condition: if condition2: function(args+1) elif condition3: print "text" return True else:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.