how to stop a function execution like...

Discussion in 'Python' started by Gaudha, Jun 16, 2009.

  1. Gaudha

    Gaudha Guest

    Is there any built-in function to stop execution of a function similar
    to stop the program execution by sys.exit?
    In the example below, I want to skip statement 2... if the 'if'
    condition is satisfied.
    Don't advice me to put statement 2 in 'else' block. That's not my
    intention.
    May be this a simple task. Sorry to say I'm novice in Python,
    gentlemen...

    def funct :
    if (.....) : statement 1
    statement 2
    Gaudha, Jun 16, 2009
    #1
    1. Advertising

  2. Gaudha wrote:

    > Is there any built-in function to stop execution of a function similar
    > to stop the program execution by sys.exit?
    > In the example below, I want to skip statement 2... if the 'if'
    > condition is satisfied.
    > Don't advice me to put statement 2 in 'else' block. That's not my
    > intention.


    Why not? It's from all you tell us perfectly the right thing to do.

    > May be this a simple task. Sorry to say I'm novice in Python,
    > gentlemen...
    >
    > def funct :
    > if (.....) : statement 1
    > statement 2



    def funct():
    if ...:
    statement 1
    return
    statement 2


    would also work. But it is not really "better" than using else.

    Diez
    Diez B. Roggisch, Jun 16, 2009
    #2
    1. Advertising

  3. Gaudha

    Gaudha Guest

    On Jun 16, 4:45 pm, "Diez B. Roggisch" <> wrote:
    > Gaudha wrote:
    > > Is there any built-in function to stop execution of a function similar
    > > to stop the program execution by sys.exit?
    > > In the example below, I want to skip statement 2... if the 'if'
    > > condition is satisfied.
    > > Don't advice me to put statement 2 in 'else' block. That's not my
    > > intention.

    >
    > Why not? It's from all you tell us perfectly the right thing to do.
    >
    > > May be this a simple task. Sorry to say I'm novice in Python,
    > > gentlemen...

    >
    > > def funct :
    > >     if (.....) : statement 1
    > >     statement 2

    >
    > def funct():
    >     if ...:
    >        statement 1
    >        return
    >     statement 2
    >
    > would also work. But it is not really "better" than using else.
    >
    > Diez


    I considered 'return' as meant only for returning any value. Thank you
    sir...
    Gaudha, Jun 16, 2009
    #3
  4. Gaudha

    pdpi Guest

    On Jun 16, 12:45 pm, "Diez B. Roggisch" <> wrote:
    > Gaudha wrote:
    > > Is there any built-in function to stop execution of a function similar
    > > to stop the program execution by sys.exit?
    > > In the example below, I want to skip statement 2... if the 'if'
    > > condition is satisfied.
    > > Don't advice me to put statement 2 in 'else' block. That's not my
    > > intention.

    >
    > Why not? It's from all you tell us perfectly the right thing to do.
    >


    If I understood his post correctly, it's because he really wants to
    exit the function early.
    If that is the case, in his real situation rather than the tiny
    example he posted, using the else clause would translate into:

    def funct(params):
    if a:
    something
    else:
    rest of the function
    goes here
    and it goes on for a while
    so you just burnt through
    an indentation level needlessly

    Now we can have a nice philosophical discussion about how using the
    else version makes the execution outline more obvious :)
    pdpi, Jun 16, 2009
    #4
  5. Gaudha

    Dave Angel Guest

    Gaudha wrote:
    > On Jun 16, 4:45 pm, "Diez B. Roggisch" <> wrote:
    >
    >> Gaudha wrote:
    >>
    >>> Is there any built-in function to stop execution of a function similar
    >>> to stop the program execution by sys.exit?
    >>> In the example below, I want to skip statement 2... if the 'if'
    >>> condition is satisfied.
    >>> Don't advice me to put statement 2 in 'else' block. That's not my
    >>> intention.
    >>>

    >> Why not? It's from all you tell us perfectly the right thing to do.
    >>
    >>
    >>> May be this a simple task. Sorry to say I'm novice in Python,
    >>> gentlemen...
    >>>
    >>> def funct :
    >>> if (.....) : statement 1
    >>> statement 2
    >>>

    >> def funct():
    >> if ...:
    >> statement 1
    >> return
    >> statement 2
    >>
    >> would also work. But it is not really "better" than using else.
    >>
    >> Diez
    >>

    >
    > I considered 'return' as meant only for returning any value. Thank you
    > sir...
    >
    >

    return with no arguments will return a value of None, same as falling
    off the end of the function. That can be important to know, as the
    caller can therefore test for None.
    Dave Angel, Jun 16, 2009
    #5
  6. Gaudha

    mzdude Guest

    On Jun 16, 7:30 am, Gaudha <> wrote:
    > Is there any built-in function to stop execution of a function similar
    > to stop the program execution by sys.exit?
    > In the example below, I want to skip statement 2... if the 'if'
    > condition is satisfied.
    > Don't advice me to put statement 2 in 'else' block. That's not my
    > intention.
    > May be this a simple task. Sorry to say I'm novice in Python,
    > gentlemen...
    >
    > def funct :
    >     if (.....) : statement 1
    >     statement 2


    sys.exit is a pretty harsh way to stop execution. It usually
    means unable to continue. There is nothing that stops you
    from putting that in a function.

    Another possiblity would be
    def funct :
    if( .... ) :
    statement 1
    raise UserWarning, "precondition X in funct not met"

    statement 2
    ...
    statement n

    Check out the try / except docs.
    mzdude, Jun 16, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wright
    Replies:
    1
    Views:
    972
    Jack Wright
    Feb 22, 2004
  2. Jack Wright
    Replies:
    2
    Views:
    6,896
    yeswanthi
    May 28, 2007
  3. Gopal Krish

    Stop further execution in Page_Load

    Gopal Krish, Oct 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    12,963
    Gopal Krish
    Oct 25, 2004
  4. TS
    Replies:
    2
    Views:
    4,266
    Steven Cheng[MSFT]
    Jan 23, 2006
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    459
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page