How do I extract specific values from **kwargs
?
I know that kwargs
is dict type,
I'm not sure if I should approach like dict or use the get()
function when approaching
People seem to write whatever they want
Shouldn't we use get()
because it's impossible to set default if we approach it like dict?
class ExampleClass:
def __init__(self, **kwargs):
self.val = kwargs['val'] #dict?
self.val2 = kwargs.get('val2') #get?
When setting the initial value in kwargs
, get()
must be used.
self.val2 = kwargs.get('val2',"default value")
The error does not occur even if val2
is not entered as a factor only when writing together
However, if you are going to set the initial value, you should not receive it with kwargs
def __init__(self, val2="default value", **kwargs):
There's no reason to insist on getting it because you can take it out and use it together.
© 2024 OneMinuteCode. All rights reserved.