Best way for function which accepts multiple types arg?

Hello again, lets take as an example a so called ‘setup_gui’ function, which accepts as arguments the desired gui to be setup, if the new gui should replace a previous gui and a tuple of varied data(as some possible guis requires additional varied information specific to them).

That second argument above accepts an int or a string which is later passed to a function called inside ‘setup_gui’, named ‘clear_gui’. If it is an str the clear_gui will look for the gui into a current guis dictionary(all guis) before deleting, if it is an int, clear_gui will delete any gui at that index into a ‘current guis list’(just stacked guis) if it is present.

I used to make use of isinstance to check if the ‘setup_gui’ second argument is an int or a str; but should I instead sepate that second argument into two ‘index, name’? Because I heard how ‘unholy’ it was to use isinstance, which the function should act as if the supplied arg is right, but what if there are multiple right alternatives(of varied types) for an arg? What about that ‘varied data tuple argument’(the last one), is it right for the function to expect for a tuple containing varied data(depending of what info a specific gui requires)? Still about the last arg, I should mention it does not make use of isinstance or any conditional checking to be used, as it was supposed to be the exact data which the desired gui to be setup requires.

Thanks for any clarification.

the best way to make a function that accepts multiple types args is not to make such a function.

These one of the very bad coding habbits coming from C language mainly, where everything tends to be big and bloated there. Thats one of the things I loved in smalltalk and squeak was the mantra that is followed , “code stupid methods” , which is also followed with functional programming.

What that means in practice is that your methods / functions should be as simple as possible and no more than 10 lines of code. That means that complicated functionality is fragmented into very simple stupid functions that one calls each other to trigger the desired result. So my advice to you is , dont do it.

Using stupid methods has the advantage that your coder will remain highly readable and highly flexible. It can be more lines of code but in the long run when your code becomes bigger and bigger it will become a blessing.

So your methods should be strick, should be doing One and only one thing , and be so stupid that even a kid can guess what they do. “One way of doing” something is also a big mantra of python , just open your python intepreter and do “import this” to see the rest of them.

The problem with varied arguments functions is that you spend loads and loads of code trying to guess what kind of data the user passes to you , and that means alot of ifs , alot of scenarios, not to exclude that you have to guess every single thing. Braking down big ugly clumsy functions to small stupid functions you restrict your user, and that might seem very bad for the user but it is not. Because the user ends up with loads and loads of very simple functions that he can use as lego bricks to assemble any kind of behavior he wants. This is actually the design I am following with my latest project and I can already see the big appeal compared to my previous addon that I used big functions for my gui .

Also having multiple functions helps you later to not having to heavily rewrite code to extend of modify your existing code.

Your text is more or less what I have read about the subject, so lets see:

1 - If I splitted that second arg(the only multi-type), into two arguments, gui_name and gui_index, so the user only supply the one which he needs, it would still go agaist the one function doing only one thing? The original second arg was the only one which used is instance/if/try-except

2 - Try-except wouldn’t be a good idea if made to act as a conditional checking? What are the uses of try-except, just for error handling?

3 - You then advise making one function which handles gui_indexes, one which handles gui_names and interface them with another function which finally setup the required guis?

1 - I guess you’re right, if it receives two arguments, but only one is selected, then we have a conditional there which could be removed.

2 - Taking in account the above, also you’re right.

3 - Seems a good alternative
Thanks for the previous clarification.