0

I'm testing to use kwargs and have a trouble. This is the python3 code :

class B:
    def __init__(self, **kwargs):
        print(kwargs['city'])

a = {'phone':'0101', 'city':'Daejeon'}
b = B(a)

But, there is an error like below :

    b = B(a)
TypeError: __init__() takes 1 positional argument but 2 were given

Is there something wrong in my code? I think that I just exactly follow the tutorial....

user3595632
  • 5,380
  • 10
  • 55
  • 111

1 Answers1

0

Keyword arguments are not passed that way.

obj1 = B(phone='0101', city='Daejeon')
obj2 = B(**a)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358