1

I have a class like:

class TestClass(object):
    def __init__(self, *args):
        try:
            ##  check some condition
        except:
            return
            ## Should exit class

    def do_something_else(self):
        ...

    def return_something(self):
        ##  return something

Now I am trying to call the class like:

TestClass(arg1, arg2, ..).do_something_else()
somthing = TestClass(arg1, arg2, ..).return_something()

When I execute the first command, my conditions fails and raise an exception. What I want is that if some exception occurs in __init__ function then do_something_method should not be called and control flow should go to the second command.

In the second command, all conditions are met and the return_something function should be called.

How can I achieve this?

2
  • 1
    This sounds like an XY problem. You don't "exit" a class – what are you actually trying to do? Commented May 5, 2017 at 10:22
  • I have a distributed task queue (celery) in which this function is called. I want to minimize the error logs in celery and store the error in the database logs instead. That's why While Initializing the class if the conditions are not met I want to store the error details in my database log and exit from the class. There are some methods which return data while others just process some other data. Otherwise, I would have called the functions from __init__ function. Commented May 5, 2017 at 10:28

3 Answers 3

4

Maybe I'm wrong, but I'd keep it simple, using a flag variable and doing this way:

class TestClass(object):
    def __init__(self, *args):
        self.flag=False
        try:
            ##  check some condition
        except:
            self.flag=True

    def do_something_else(self):
        if self.flag:
            #do what you want, e.g. call a second command
            return
        ...

    def return_something(self):
        ##  return something
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest you to handle the exceptional condition in a separate function rather than inside the constructor

Instead of

TestClass(arg1, arg2, ..).do_something_else()

do

try:
    obj = TestClass(arg1,arg2)
except:
     pass
else:
     obj.do_something_else()

And remove the try/except statement from the init method.
You shouldn't return anything from __init__ method.

Comments

-1

You can just create an Object of the class TestClass and return "True" from try block and "False" from except block. Check if the value is True or Flase and execute the the required function. Creating an object will automatically triiger the init method and return true or false based on your condition. Check that returned value to decide whether to execute required method or not.

2 Comments

It's not clear what you mean. Also, __init__ must always return None.
My answer is somewhat similar to the first answer in this post. But I am not sure if init must always return 'None'. Can you clarify it a bit more sir?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.