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


Groups > comp.lang.python > #88036

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

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <webmailgroups@gmail.com>
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; 'read.': 0.03; 'skip:[ 20': 0.04; 'argument': 0.05; 'elif': 0.05; 'defaults': 0.07; 'incompatible': 0.07; 'tries': 0.07; 'arguments': 0.09; 'function,': 0.09; 'function:': 0.09; 'subject:Function': 0.09; 'def': 0.12; 'wrote': 0.14; 'combinations': 0.16; 'definition.': 0.16; 'exist.': 0.16; "function's": 0.16; "guido's": 0.16; 'ivan': 0.16; 'occurs.': 0.16; 'subject:avoiding': 0.16; 'subject:combinations': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; 'folder': 0.16; 'thursday,': 0.16; 'all.': 0.16; 'do,': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'message-----': 0.19; 'split': 0.19; 'thu,': 0.19; 'input': 0.22; 'example': 0.22; 'separate': 0.22; 'error': 0.23; 'instance,': 0.24; 'rid': 0.24; 'file.': 0.24; 'looks': 0.24; '(or': 0.24; 'question': 0.24; 'define': 0.26; 'this:': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'to:2**1': 0.27; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'related': 0.29; 'sets': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'that.': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'file:': 0.31; 'steven': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'url:python': 0.33; 'could': 0.34; 'basic': 0.35; 'common': 0.35; 'possible.': 0.35; '(2)': 0.35; 'case,': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'combination': 0.36; 'much.': 0.36; 'url:listinfo': 0.36; 'shows': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'email addr:python.org': 0.37; 'message-id:@gmail.com': 0.38; '(3)': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'previous': 0.38; 'little': 0.38; 'subject:': 0.39; 'skip:b 40': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'read': 0.60; 'full': 0.61; 'march': 0.61; 're:': 0.63; 'spam.': 0.64; 'more': 0.64; 'email name:python- list': 0.65; 'needing': 0.65; 'occur': 0.65; 'within': 0.65; 'mar': 0.68; '26,': 0.68; 'mail,': 0.68; 'reads': 0.68; 'default': 0.69; '2015': 0.84; 'received:192.114': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-type:content-transfer-encoding:thread-index :content-language; bh=KmKDMFMn17NqS9hc0iux921riHcUHL6swE7xj2S2f8A=; b=Zb/N09CNr4tS7k1DW2AXw/tLkSPJEiBlFbpSw3XjostJNFs0yfPUW1gZYgdUSrPaJD N70D6SfzKTd/qPR8R6lq7EixxrMDHyTbo79Ho3dU8673yF51TlhpHz3SANmbnr6P0cVa AdU4YxCZI0y8uRYzEeV1pO+IqyvM++Zp2NxM/0893z3ooEuEwi8bebJ1b/Otcg0957rR Cc38YJTohTBNA+W8ple9zoNLccRhp+t79I0qnR4NcZYWsfCJPYHRIsQNQNNuhzfe8OuM 57PVe8j1XFTP9wYqEyoLUIzNUGQeau7SW2ewQkEygm6qba0+jpLRB1EmtOJ3OaBIE+MM gQoA==
X-Received by 10.180.212.37 with SMTP id nh5mr45003135wic.76.1427363278443; Thu, 26 Mar 2015 02:47:58 -0700 (PDT)
From "Ivan Evstegneev" <webmailgroups@gmail.com>
To "'Steven D'Aprano'" <steve+comp.lang.python@pearwood.info>, <python-list@python.org>
References <mailman.146.1427305418.10327.python-list@python.org> <55134964$0$13004$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <55134964$0$13004$c3e8da3$5496439d@news.astraweb.com>
Subject RE: Function Defaults - avoiding unneccerary combinations of arguments at input
Date Thu, 26 Mar 2015 11:47:54 +0200
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding 7bit
X-Mailer Microsoft Outlook 14.0
Thread-Index AQHIxDzj7JvBp+BRgOPLDzFQIIsTCwHNEjV6nS9gNDA=
Content-Language en-us
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.191.1427363280.10327.python-list@python.org> (permalink)
Lines 118
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427363280 news.xs4all.nl 2973 [2001:888:2000:d::a6]:51656
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88036

Show key headers only | View raw



> -----Original Message-----
> From: Python-list [mailto:python-list-
> bounces+webmailgroups=gmail.com@python.org] On Behalf Of Steven
> D'Aprano
> Sent: Thursday, March 26, 2015 01:49
> To: python-list@python.org
> Subject: Re: Function Defaults - avoiding unneccerary combinations of
> arguments at input
> 
> On Thu, 26 Mar 2015 04:43 am, 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
> 
> 
> This is an extreme example that shows why Guido's Law "No constant
> arguments" is a good design principle. (Well, it's not really so much a
law as a
> guideline.)
> 
> If you have a function that looks like this:
> 
> def spam(arg, flag=True):
>     if flag:
>         return do_this(arg)
>     else:
>         return do_that(arg)
> 
> 
> then you should just use do_this and do_that directly and get rid of spam.
> 
> In your case, its hard to say *exactly* what you should do, since you are
only
> showing a small sketch of "my_fun", but it looks to me that it tries to do
too
> much. You can probably split it into two or four smaller functions, and
avoid
> needing so many (or any!) flags.
> 
> That will avoid (or at least reduce) the need to check for mutually
> incompatible sets of arguments.
> 
> Another useful design principle: if dealing with the combinations of
> arguments is too hard, that's a sign that you have too many combinations
of
> arguments.
> 
> If there are combinations which are impossible, there are three basic ways
to
> deal with that. In order from best to worst:
> 
> 
> (1) Don't let those combinations occur at all. Redesign your function, or
split
> it into multiple functions, so the impossible combinations no longer
exist.
> 
> (2) Raise an error when an impossible combination occurs.
> 
> (3) Just ignore one or more argument so that what was impossible is now
> possible.
> 
> 
> 
> 
> --
> Steven
> 
> --
> https://mail.python.org/mailman/listinfo/python-list



Hello Steven,

As I said in a previous mail, main purpose of this arguments is to define
what path should be chose. It is actually one xls file that could be placed
into various placed within my folder tree. 

For instance, I have following folder tree:

data_lib/
	current/
	history/
	built/

Say  I have "test.xls" that could be in one of those three folders. 
I wrote a function that reads it out, and its input arguments just define
where it should be read. So  all those "ifs" related to path definition.



Still now I'm thinking to really split it out...  I'll have a separate
function for path definition and then it will call a common reader_fun() in
order to read this file.


Sincerely,

Ivan


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


Thread

Function Defaults - avoiding unneccerary combinations of arguments at input "Ivan Evstegneev" <webmailgroups@gmail.com> - 2015-03-25 19:43 +0200
  Re: Function Defaults - avoiding unneccerary combinations of arguments at input Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-26 10:48 +1100
    RE: Function Defaults - avoiding unneccerary combinations of arguments at input "Ivan Evstegneev" <webmailgroups@gmail.com> - 2015-03-26 11:47 +0200
      RE: Function Defaults - avoiding unneccerary combinations of arguments at input Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-26 21:51 +1100

csiph-web