Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'elif': 0.05; '*not*': 0.07; 'exception,': 0.09; 'excluding': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Function': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'-'": 0.16; '(read': 0.16; 'combinations': 0.16; 'googled': 0.16; 'ivan': 0.16; 'occurs.': 0.16; 'proportion': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'redundant': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:avoiding': 0.16; 'subject:combinations': 0.16; 'valueerror,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'passing': 0.19; 'command': 0.22; 'header:User- Agent:1': 0.23; 'case.': 0.24; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'correct': 0.29; 'possibility': 0.29; 'raise': 0.29; 'ignored.': 0.30; 'code': 0.31; 'file:': 0.31; 'implemented': 0.33; 'except': 0.35; 'one,': 0.35; 'but': 0.35; 'there': 0.35; 'combination': 0.36; 'should': 0.36; 'too': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'catch': 0.60; 'ian': 0.60; 'most': 0.60; 'full': 0.61; 'first': 0.61; 'here': 0.66; 'invalid': 0.68; 'exclusive': 0.81; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Function Defaults - avoiding unneccerary combinations of arguments at input Date: Wed, 25 Mar 2015 16:19:35 -0400 References: <009d01d06723$38860d50$a99227f0$@gmail.com> <000801d06734$f9e57830$edb06890$@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 In-Reply-To: <000801d06734$f9e57830$edb06890$@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427314812 news.xs4all.nl 2935 [2001:888:2000:d::a6]:49684 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87978 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