Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87978
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Function Defaults - avoiding unneccerary combinations of arguments at input |
| Date | 2015-03-25 16:19 -0400 |
| References | <009d01d06723$38860d50$a99227f0$@gmail.com> <meuvkb$e5t$1@ger.gmane.org> <000801d06734$f9e57830$edb06890$@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.160.1427314812.10327.python-list@python.org> (permalink) |
On 3/25/2015 3:50 PM, Ivan Evstegneev wrote:
> Googled a bit, and found only one, a "ValueError" exception, but still don't
> understand how it should be implemented in my case.
>
> Should my code look like this one:
>
> def my_fun(history=False, built=False, current=False, topo=None,
> full=False, file=None):
> try:
> if currnet and full:
> do something_1
> elif current and file:
> do something_2
> elif history and full and file:
> do something_3
>
> except ValueError:
> print("No valid input! Please try again ...")
You need to raise ValueError, not try to catch one that never occurs.
def my_fun(history=False, built=False, current=False, topo=None,
full=False, file=None):
if current and full:
do something_1
elif current and file:
do something_2
elif history and full and file:
do something_3
else:
raise ValueError("not a valid combination of arguments")
My answer does not negate the possibility that Ian is correct that you
are trying to do too much in one function. But I did want to point out
that there are stdlib precedents for excluding invalid argument
combinations. However, in those cases, only a small proportion of
combinations are invalid and if the function were split, most of the
code would be duplicated.
Here is an mutually exclusive combo that is *not* flagged: passing both
'-' (read from stdin) and 'file.py' to python on the command line. The
first wins and the second is ignored. '-i -' is redundant and ignored.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Function Defaults - avoiding unneccerary combinations of arguments at input Terry Reedy <tjreedy@udel.edu> - 2015-03-25 16:19 -0400
csiph-web