Function Defaults - avoiding unneccerary combinations of arguments at input

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 26 06:51:01 EDT 2015


On Thu, 26 Mar 2015 08:47 pm, Ivan Evstegneev wrote:

> 
> 
>> -----Original Message-----
>> From: Python-list [mailto:python-list-
>> bounces+webmailgroups=gmail.com at python.org] On Behalf Of Steven
>> D'Aprano
>> Sent: Thursday, March 26, 2015 01:49
>> To: python-list at 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

[...]

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

Perhaps something like this?

def my_func(subdir):
    if subdir not in ("current", "history", "built"):
        raise ValueError("invalid sub-directory")
    # Better to use os.path.join, but I'm feeling lazy.
    path = "path/to/data_lib/" + subdir + "/test.xls"
    process(path)




-- 
Steven




More information about the Python-list mailing list