Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Arbitrary Keyword Arguments (**kwargs) | Function Arguments in Details
Mastering Python: Annotations, Errors and Environment

bookArbitrary Keyword Arguments (**kwargs)

メニューを表示するにはスワイプしてください

Arbitrary Keyword Arguments (**kwargs) are arbitrary arguments with name. Syntax of the keyword argument (keyword=argument). The **kwargs syntax allows us to pass a different number of named arguments.

12345
def some_function(**kwargs): print(type(kwargs)) print(kwargs) some_function(first=11, second=22, some=33)
copy

In the example above, we pass the named arguments (first=11 and other) to the some_function().
The taken kwargs is a dict (dictionary) where:

  • keys are taken keywords in the str type.
  • values are values of these keywords.
key (keyword)value
first11
second22
some33

The operations with keywords are regular dict operations. You can use the keys() dictionary method to get all taken keywords and use the items() to get the key-value pairs:

1234567
def user_info(**kwargs): print("Taken info:", kwargs.keys()) for key, value in kwargs.items(): print(key + ":", value) user_info(name="John", surname="Smith", age="16", username="josmith16")
copy

1. What do you need to use to take a tuple of optional arguments?

2. What do you need to use to take a dict of keyword arguments?

question mark

What do you need to use to take a tuple of optional arguments?

正しい答えを選んでください

question mark

What do you need to use to take a dict of keyword arguments?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  4
some-alt