For the ease of further scale, I define a class Book
with args
and 'kwargs'.
class Book:
def __init__(self, *args, **kwargs):
if args:
self.name,\
self.author,\
= args
elif kwargs:
self.__dict__.update(kwargs)
It works well respectively with positional and keywords arguments
In [62]: book1 = Book('Python', 'Guido')
In [63]: book1.author
Out[63]: 'Guido'
In [65]: book2 = Book(name='Python', author='Guido')
In [66]: book2.name
Out[66]: 'Python'
When test with mixture of positional and keywords arguments,error reports.
In [67]: book3 = Book('Python', author='Guido')
ValueError: not enough values to unpack (expected 2, got 1)
The bug can be fixed with multiple conditions or a standard definition class
without taking *args
or 'kwargs'.
How to fix it in an elegant method?