Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26009

Re: assert expressions

References <9f6bed70-1ec1-4e3e-bdd0-c96013bf7df2@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-07-24 14:31 -0600
Subject Re: assert expressions
Newsgroups comp.lang.python
Message-ID <mailman.2553.1343161917.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jul 24, 2012 at 1:57 PM, Wanderer <wanderer@dialup4less.com> wrote:
> If I use the code
>
> assert False, "unhandled option"
>
> I get output like:
>
> option -q not recognized
> for help use --help
>
> What other expressions can I use other than "unhandled option"? Is there a list somewhere?

Are you using argparse or optparse or getopt or something else
altogether?  And where are you placing this assert?  It would be
helpful to see some actual code to understand what you are doing.

And by the way, assert is a very bad way to check user input or to
unconditionally raise an exception.  The reason is that if Python is
invoked with -O, then all assertions are removed from the compiled
bytecode, and then your unconditional exception code doesn't raise any
exception at all.  If you want to raise an exception, just do it:

raise Exception("unhandled option")

Ideally, you would also subclass Exception to create a more specific
exception class for your custom exception:

class UnhandledOptionException(Exception):
    pass

# Then, later on...

raise UnhandledOptionException("-q")

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

assert expressions Wanderer <wanderer@dialup4less.com> - 2012-07-24 12:57 -0700
  Re: assert expressions Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-24 14:31 -0600
    Re: assert expressions Wanderer <wanderer@dialup4less.com> - 2012-07-24 13:47 -0700
      Re: assert expressions Wanderer <wanderer@dialup4less.com> - 2012-07-24 13:56 -0700
    Re: assert expressions Wanderer <wanderer@dialup4less.com> - 2012-07-24 13:44 -0700
      Re: assert expressions Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-24 15:22 -0600
        Re: assert expressions Wanderer <wanderer@dialup4less.com> - 2012-07-24 14:25 -0700

csiph-web