I have a function that just does a batch of calls to a couple other functions for simplicity's sake and it's args/kwargs are basically just the args/kwargs of the inside functions. If my inside function_1 has two optional kwargs that I may set in the initial function call, how can I pass both those two to it in the definition without accidentally overriding any defaults that function might use for those kwargs if MY kwargs are None? I saw a SO answer that explained how to do it with 1 kwargs " **(a_kwarg or {}) " but how do I extend that expression to account for one/both of my kwargs possibly being none? Can I do that in a short expression or should I just iterate over my kwargs and pass in a dict that has only the not-None kwargs?
#๐ Succinct way to pass two possible kwargs to function inside function
14 messages ยท Page 1 of 1 (latest)
@lost hare
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
You could possibly use filter on your kwargs to only get not-None values or to explicitly pass specific values from yout kwargs to each inner function, you could do something like
kwargs.get(arg) or default_value, etc.
You can build the kwargs dict from scratch in the wrapper function, checking each argument (same proposition than above).
Yeah I was thinking I should just make the kwargs filtering explicit instead of trying to be cheeky with a one-liner
yeah a match case statement makes it very clear imo
Gotta think of future-Yiggs
Since I only have two kwargs at the moment I just did a dict comprehension of {k, v for k, v in {inner_function_kwarg: outer_kwarg_value, }.items() if v is not None}
I don't claim this is the best solution, but in similar situations, when I want to avoid repeating the definition of default values, I put them all in a little function that sets them, instead of in the inner or outer functions' headers, and call it at the start of both, assignign the result to the args or mutating a kwargs dict.
i dont quite get the issue, if you just send in a dict the process could be simplified.
func a(dict)
get needed vars from dict,
process vars and update dict if needed.
func a(dict)
inner function(dict)
extract needed vars
update and return dict, either same dict or dict with updated k:v.
at the return of func a you can pack the results together or create an entirely new dict to only return the result vars you need..
unless Im missing the entire point.
like I usually use a running_params dict to keep a base setting thru the entire program, updating a key for func responses that alter needs of the script during the run.
ofc never returning the actual dict in use but updating based on a output dict per function.
like,
run_params{
"settings": {
"workers": 10
},
"progress":{
"func1": True
},
"results":{
"func1": results
}}
this way if I see something in the log i dont like I can set a break point and see the actual data, or just to inspect the data without dumping to the log.. cause that gets messy
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.