r/pythonhelp • • May 09 '23

args/kwargs ordering and defaults.

I have a method set() which accepts the possibility of a index and text.

def set(self, index:str|int|float=None, text:str="", **kwargs):
print(index, text)

My issue is if only 1 arg is passed i want it to be set to 'text' however if 2 or more are passed then it should be set to index, text then kwargs.

example uses:

set(0, 'some text', colour='blue') #-> index=0, text='some text', kwargs{'colour':'blue'}
set('some text') #-> index=None, text='some text'
set('end', 'some text') #-> index='end', text='some text'
set(1.0, 'some text') #-> index=1.0, text='some text'
set(text='some text') #-> index=0, text='some text'
3 Upvotes

10 comments sorted by

2

u/sentles May 09 '23

One way is to set the default value of text to None. At the start of your function, check if text is None. If it is, set text = index, and set index to whatever default value you use when it isn't passed.

Another way is to instead not use arguments index and test, but just use *args. If the length of args is 1, use the argument as text, else if it is 2, use them as index and text respectively.

I'd prefer the former, or course, since it's clearer what arguments are expected.

1

u/AceScottieAC3 May 10 '23

I cannot do the first solution as this beraks the compatability i am trying to keep. It is always (index, text) or (text) but never (text, index).

The second solution breaks type hinting.

The solution i have (and dont like either) is:

def set(self, index:int|str|float=None, text:str=None):
    if text is None:
        text = index
        index = None

1

u/sentles May 10 '23

The first solution I provided is the same as the one you posted.

1

u/AceScottieAC3 May 10 '23

my bad, i read "set the default value of text to None. At the start" as set text to None at the start. e.g. def set(text=None, index):

1

u/Goobyalus May 09 '23

On an unrelated note, is this a function or a method?

I would avoid shadowing the builtin set.

1

u/AceScottieAC3 May 10 '23

its a class method

1

u/Goobyalus May 10 '23

Actually I take back my previous comment. singledispatch works off the type of the first argument, so if str is valid for the first argument in both cases, it won't work for you.

1

u/AceScottieAC3 May 10 '23

If its possible to change the target of singledispactch check to the text arg then i could use

from functools import singledispatch
NoneType = type(None)
@singledispatch
def test(index, text):
    print(index, text)
@test.register(str)
def _(index, text=None):
    print(index, text, 'str')
@test.register(NoneType)
def _(index, text:None=None):
    print(index, text, 'None')

1

u/Goobyalus May 10 '23

You could roll your own dispatch, but it looks to me like the builtin one explicitly uses the first positional argument's type:

https://github.com/python/cpython/blob/main/Lib/functools.py#L909

1

u/Goobyalus May 09 '23

Probably the best way would be with functools' singledispatch

https://docs.python.org/3/library/functools.html#functools.singledispatch