D
D
DUDE2021-08-19 13:26:11
Python
DUDE, 2021-08-19 13:26:11

How to check function/method arguments?

Hello everyone, there is a certain method or function that accepts a limited number of incoming arguments:

def foo(a: str = None, b: str = None, c: int = None):
    ...


Suppose there are several processing scenarios depending on what arguments are given.
  • a != None, b != None, c == None
  • a == None, b == None, c!= None


As an option, you can break it by ifs with the corresponding exceptions, but I'm not sure if this is acceptable, and if it is acceptable, then write validation inside the function?

Maybe I'm doing something wrong, if I encounter this ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-08-19
@Vindicar

If the argument is None, does that mean some default value should be used? If yes, then you can take out if on this argument separately, and then behave as if the argument was given.
If not, then you should ask yourself - isn't this function taking on too much? Shouldn't it be divided into several?

S
Stefan, 2021-08-19
@MEDIOFF

Rewrite the function so that it would work normally with None. Just as for me, if your function may not take arguments at all, then it is more logical to pack everything into:

def foo(*args, **kwargs):
    ...

And perhaps just with such logic it will be easier for you to arrange all this
. In general, everything strongly depends on what you are doing inside the function, I only told you my recommendations without relying on the requirements, and this may make adjustments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question