Class returning None ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Marshall

    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.

  • Cliff Wells

    #2
    Re: Class returning None ?

    On Mon, 2004-01-26 at 00:43, George Marshall wrote:[color=blue]
    > Hi, I'm wondering, what's the best way to check for
    > success or failure on object initialization ?[/color]

    Raise an exception.

    [color=blue]
    > This is my ugly workaround to do that :[/color]

    [snip]
    [color=blue]
    > What's the python way to do that ?[/color]

    from exceptions import Exception

    class SomeError(Excep tion): 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


    Comment

    • Aahz

      #3
      Re: Class returning None ?

      In article <mailman.791.10 75107363.12720. python-list@python.org >,
      Cliff Wells <clifford.wells @comcast.net> wrote:[color=blue]
      >
      >from exceptions import Exception
      >class SomeError(Excep tion): pass[/color]

      BTW, there's no need to do this. ``Exception`` lives in the built-in
      namespace.
      --
      Aahz (aahz@pythoncra ft.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

      Comment

      • Cliff Wells

        #4
        Re: Class returning None ?

        On Mon, 2004-01-26 at 06:33, Aahz wrote:[color=blue]
        > In article <mailman.791.10 75107363.12720. python-list@python.org >,
        > Cliff Wells <clifford.wells @comcast.net> wrote:[color=green]
        > >
        > >from exceptions import Exception
        > >class SomeError(Excep tion): pass[/color]
        >
        > BTW, there's no need to do this. ``Exception`` lives in the built-in
        > namespace.[/color]

        Er, explicit is better than implicit? ;-)

        Somehow I never noticed that.

        Regards,
        Cliff

        --
        So sad, love lies there still
        -Bauhaus


        Comment

        Working...