Is there a (clean, easy) way to set keyword args as other keyword args in a parameters list? Something like this:
class Foo():
def __init__(self, kwarg1='a', kwarg2=kwarg1, kwarg3=kwarg1):
I basically need all three to have the same default value without hard coding it in. That way if the user wants to edit kwarg1
to be something else, the default of kwarg2
and kwarg3
will default to the new value as well; but will still allow the functionality of customizing kwarg2
and kwarg3
if desired.
I imagine I can do something like this:
class Foo():
def __init__(self, kwarg1='a', kwarg2='', kwarg3=''):
if kwarg2 == '':
self.kwarg2 = kwarg1
if kwarg3 == '':
self.kwarg3 = kwarg1
It's just kind of ugly. I know this is an odd question. Just kinda curious if there is a cleaner way to accomplish this.
Try this, Setting value None
to kwarg1
and kwarg2
might help you.:
class Foo:
def __init__(self,kwarg1='a',kwarg2=None,kwarg3=None):
self.kwarg1 = kwarg1
self.kwarg2 = kwarg2 if kwarg2 != None else self.kwarg2 = kwarg1
self.kwarg3 = kwarg3 if kwarg3 != None else self.kwarg3 = kwarg1