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


Groups > comp.lang.python > #87972 > unrolled thread

RE: Function Defaults - avoiding unneccerary combinations of arguments at input

Started by"Ivan Evstegneev" <webmailgroups@gmail.com>
First post2015-03-25 21:50 +0200
Last post2015-03-25 22:43 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  RE: Function Defaults - avoiding unneccerary combinations of arguments at input "Ivan Evstegneev" <webmailgroups@gmail.com> - 2015-03-25 21:50 +0200
    Re: Function Defaults - avoiding unneccerary combinations of arguments at input Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-03-25 20:07 +0000
      RE: Function Defaults - avoiding unneccerary combinations of arguments at input "Ivan Evstegneev" <webmailgroups@gmail.com> - 2015-03-25 22:43 +0200

#87972 — RE: Function Defaults - avoiding unneccerary combinations of arguments at input

From"Ivan Evstegneev" <webmailgroups@gmail.com>
Date2015-03-25 21:50 +0200
SubjectRE: Function Defaults - avoiding unneccerary combinations of arguments at input
Message-ID<mailman.158.1427313051.10327.python-list@python.org>
Hello again ^_^,

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 ...")

	
	...... some code here..............


Or I'm just too optimistic ^_^? 

Thanks in advance,

Ivan.

> -----Original Message-----
> From: Python-list [mailto:python-list-
> bounces+webmailgroups=gmail.com@python.org] On Behalf Of Terry Reedy
> Sent: Wednesday, March 25, 2015 20:43
> To: python-list@python.org
> Subject: Re: Function Defaults - avoiding unneccerary combinations of
> arguments at input
> 
> On 3/25/2015 1:43 PM, Ivan Evstegneev wrote:
> > Hello all ,
> >
> >
> > Just a little question about function's default arguments.
> >
> > Let's say I have this function:
> >
> > def  my_fun(history=False, built=False, current=False, topo=None,
> > full=False, file=None):
> > 	if currnet and full:
> > 		do something_1
> > 	elif current and file:
> > 		do something_2
> > 	elif history and full and file:
> > 		do something_3
> >
> >
> > 	...... some code here..............
> >
> > and so on...
> >
> > I won't cover all the possibilities here (actually I don't use all of
> > them ^_^).
> >
> > The question is about avoiding the response for unnecessary
> combinations?
> >
> > For instance, if user will call function this way:
> >
> >
> >>>> my_fun(current=True, full=True, topo='some str', file="some_file")
> >
> > That will lead to function's misbehavior. As a particular case it will
> > choose "current and full" condition.
> >
> >
> > What is the common accepted way to deal with such unwanted situations?
> 
> Raise a value error for illegal combinations.  There are a few instances
in the
> stdlib where 2 of several options are mutually incompatible.
> 
> 
> --
> Terry Jan Reedy
> 
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [next] | [standalone]


#87974

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-03-25 20:07 +0000
Message-ID<mev4he$amn$5@dont-email.me>
In reply to#87972
On Wed, 25 Mar 2015 21:50:40 +0200, Ivan Evstegneev wrote:

> Hello again ^_^,
> 
> 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 ...")
> 
> 
> 	...... some code here..............
> 
> 
> Or I'm just too optimistic ^_^?
> 
> Thanks in advance,
> 
> Ivan.
> 

A) Please don't top-post.  Put your new stuff below previous context, so 
it flows in readable order.

B) You've got the concept backwards.  You're not trying to catch a 
ValueError because one came up below your function.  Your function was 
called incorrectly, and you're kicking the responsibility for dealing 
with it back to the guy who got it wrong.

def my_fun(....):
    if invalid_argument_combination:
       raise ValueError('Hey jerk, read the documentation.')

    # otherwise you do your thing in here.


-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#87981

From"Ivan Evstegneev" <webmailgroups@gmail.com>
Date2015-03-25 22:43 +0200
Message-ID<mailman.161.1427316235.10327.python-list@python.org>
In reply to#87974

> -----Original Message-----
> From: Python-list [mailto:python-list-
> bounces+webmailgroups=gmail.com@python.org] On Behalf Of Rob Gaddi
> Sent: Wednesday, March 25, 2015 22:07
> To: python-list@python.org
> Subject: Re: Function Defaults - avoiding unneccerary combinations of
> arguments at input
> 
> On Wed, 25 Mar 2015 21:50:40 +0200, Ivan Evstegneev wrote:
> 
> > Hello again ^_^,
> >
> > 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 ...")
> >
> >
> > 	...... some code here..............
> >
> >
> > Or I'm just too optimistic ^_^?
> >
> > Thanks in advance,
> >
> > Ivan.
> >
> 
> A) Please don't top-post.  Put your new stuff below previous context, so
it
> flows in readable order.
> 
> B) You've got the concept backwards.  You're not trying to catch a
ValueError
> because one came up below your function.  Your function was called
> incorrectly, and you're kicking the responsibility for dealing with it
back to the
> guy who got it wrong.
> 
> def my_fun(....):
>     if invalid_argument_combination:
>        raise ValueError('Hey jerk, read the documentation.')
> 
>     # otherwise you do your thing in here.
> 
> 
> --
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email
> address domain is currently out of order.  See above to fix.
> --
> https://mail.python.org/mailman/listinfo/python-list




Ok thanks fo the answer. Got it. About top-posting... will need to tweak
Outlook settings.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web