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


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

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

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-03-25 12:00 -0600
Last post2015-03-25 12:00 -0600
Articles 1 — 1 participant

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 Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-25 12:00 -0600

#87958 — Re: Function Defaults - avoiding unneccerary combinations of arguments at input

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-03-25 12:00 -0600
SubjectRe: Function Defaults - avoiding unneccerary combinations of arguments at input
Message-ID<mailman.148.1427306867.10327.python-list@python.org>
On Wed, Mar 25, 2015 at 11:43 AM, Ivan Evstegneev
<webmailgroups@gmail.com> 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?

Don't try to combine all these into a single function whose behaviour
is controlled by a bunch of booleans. Create a separate function
instead for each unique intended behavior (possibly sharing a common
non-public implementation).

[toc] | [standalone]


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


csiph-web