Question on keyword arguments

Peter Otten __peter__ at web.de
Thu Feb 18 09:37:16 EST 2016


grsmith at atlanticbb.net wrote:

> Would this be the correct way to return
> a list as a default result.

If it does what you want it to do it is "correct" as in "complies with 
specification".
 
> Also, would the list be the preferable result (to a python programmer) ?

A list as a return value is OK as is any other object, however,

> def test(command, return_type='LIST'):

passing the desired result type as an argument is usually not a good idea. 

>     """ Go to database and return data"""
>     if return_type == 'LIST':
>         result = ['ONE', 'TWO', 'THREE']
>     else:
>         result = r'0xfeONE\0exfeTWO\0xfeTHREE'
>     return result
> 
> if __name__ == '__main__':
>     print(test('cmd'))
>     print(test('cmd', 'LIST'))
>     print(test('cmd', None))
>     print(test('cmd', 'string'))

Make it separate functions instead:

def test(command):
    return ["ONE", "TWO", "THREE"]

def test_as_string(command, sep=","):
    return sep.join(test(command))

if __name__ == '__main__':
    print(test("cmd"))
    print(test_as_string("cmd"))





More information about the Python-list mailing list