Using a keyword argument when creating an instance of a class, followed by *args
, leads to an error. However, without this, the readability is impaired. Notice the a= when creating the instance in the second example.
This works:
class ExampleClass:
def __init__(self, a, *args):
self.a = a
args = [1]
example_instance = ExampleClass(None, *args)
print(example_instance.a)
>>> None
While this does not work:
class ExampleClass:
def __init__(self, a, *args):
self.a = a
args = [1]
example_instance = ExampleClass(a=None, *args)
print(example_instance.a)
>>> Traceback (most recent call last):
>>> File "C:/Users/test.py", line 63, in <module>
>>> example_instance = ExampleClass(a=None, *args)
>>> TypeError: __init__() got multiple values for argument 'a'