함수 인자

def myf(**args):
    args['myname']='nck'
    for i in args:
        print(i)
    print(type(args))
    print(args)
myf(a=1,b=3)
  a
b
myname
<class 'dict'>
{'a': 1, 'b': 3, 'myname': 'nck'}

args가 사전으로 역할을 함, 이 사전에 추가등을 할 수 있음

test={'c':1,'d':6}
myf(test)
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  <ipython-input-8-2e9661a3b0f2> in <module>()
----> 1 myf(test)

  TypeError: myf() takes 0 positional arguments but 1 was given
myf(**test)
  c
d
myname
<class 'dict'>

test를 언팩킹하여 넣어줘야함

a,b=1,2
a
  1
b
  2
c=1,4
c
  (1, 4)
def packing(a,b):
    return a,b
packing(5,6)
  (5, 6)
1,2
  (1, 2)

packing이 됨.

dic1={'a':1,'b':2}
dic2={'c':3,'d':4}
dic1+dic2
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  <ipython-input-5-a503c9b1cf15> in <module>()
----> 1 dic1+dic2

  TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
dict()
  {}
dict(**dic1)
  {'a': 1, 'b': 2}
dict(**dic1,**dic2)
  {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Tags:

Updated:

Leave a Comment