I have function which has 10 argument but each time i call a function i dont need to pass all the 10 arguments as all the 10 arguments are not relevant each time the function is called.
def function(arg1:Optional[int] =0,arg2:Optional[int] =0,arg3:Optional[int] =0,
arg4:Optional[int] =0,arg5:Optional[int] =0, arg6:Optional[str] =None,
arg7:Optional[str] =None,arg8:Optional[str]=None,arg9:Optional[str]=None,arg10:Optional[str] =None)
pass
How to convert these arguments into a class and pass that class in the function as an argument each time the function needs to be called so that only relevant arguments get passed each time the function is called?
If any one can give some hints pls.
CodePudding user response:
Here's how you might bundle all of those argument values into a dataclass
:
from dataclasses import dataclass
from typing import Optional
@dataclass
class FunctionArgs:
arg1: Optional[int] = 0
arg2: Optional[int] = 0
arg3: Optional[int] = 0
arg4: Optional[int] = 0
arg5: Optional[int] = 0
arg6: Optional[str] = None
arg7: Optional[str] = None
arg8: Optional[str] = None
arg9: Optional[str] = None
arg10: Optional[str] = None
def function(args: FunctionArgs) -> None:
pass
You'd then call the function like this:
function(FunctionArgs(arg4=10))