Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tamago324/46ffe72c2d7258a66b756e5d1477be83 to your computer and use it in GitHub Desktop.

Select an option

Save tamago324/46ffe72c2d7258a66b756e5d1477be83 to your computer and use it in GitHub Desktop.

以下のようにyは渡さないで、kwargsに渡したいとき

def test(x, y=None, **kwargs):
    print(f'x: {x}')
    print(f'y: {y}')
    print(f'kwargs: {kwargs}')

# yは渡さないで、kwargsに渡したいとき
test(1, a=2, b=3)
x: 1
y: None
kwargs: {'a': 2, 'b': 3}

a=2, b=3ではなく、辞書を渡すにはどうすればいいのかわかんない...

**で渡す!!!

d = {'a': 2, 'b': 3}
test(1, **d)    # test(1, a=2, b=3)と同じ意味になる?
x: 1
y: None
kwargs: {'a': 2, 'b': 3}

辞書に**をつけることで展開できる

参考文献

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment