This question already has an answer here:
这个问题已经有了答案:
- Passing a dictionary to a function in python as keyword parameters 4 answers
- 将字典作为关键字参数4的答案传递给python中的函数
My code
我的代码
1st file:
文件1:
data = {'school':'DAV', 'standard': '7', 'name': 'abc', 'city': 'delhi'}
my_function(*data)
2nd file:
文件2:
my_function(*data):
schoolname = school
cityname = city
standard = standard
studentname = name
in the above code, only keys of "data" dictionary were get passed to my_function()
, but i want key-value pairs to pass. How to correct this ?
在上面的代码中,只有“data”字典的键被传递给my_function(),但是我想传递键-值对。如何改正?
I want the my_function()
to get modified like this
我想要像这样修改my_function()
my_function(school='DAV', standard='7', name='abc', city='delhi')
and this is my requirement, give answers according to this
这是我的要求,根据这个给出答案
EDIT: dictionary key class is changed to standard
编辑:字典键类被更改为标准
3 个解决方案
#1
75
If you want to use them like that, define the function with the variable names as normal (but use klass
for class
, you can't use reserved words):
如果你想这样使用它们,用变量名来定义函数(但是使用klass作为类,你不能使用保留字):
def my_function(school, city, klass, name):
schoolname = school
cityname = city
standard = klass
studentname = name
Now (as long as you rename class to klass in your dictionary) you can use **
when you call the function:
现在(只要在字典中将类重命名为klass),就可以在调用函数时使用**:
my_function(**data)
and it will work as you want.
你想怎么做就怎么做。
#2
28
*data interprets arguments as tuples, instead you have to pass **data which interprets the arguments as dictionary.
*数据将参数解释为元组,相反,您必须传递**数据,该数据将参数解释为字典。
data = {'school':'DAV', 'class': '7', 'name': 'abc', 'city': 'pune'}
def my_function(**data):
schoolname = data['school']
cityname = data['city']
standard = data['class']
studentname = data['name']
You can call the function like this:
你可以这样调用函数:
my_function(**data)
#3
6
You can just pass it
你可以通过它
def my_function(my_data):
my_data["schoolname"] = "something"
print my_data
or if you really want to
或者如果你真的想
def my_function(**kwargs):
kwargs["schoolname"] = "something"
print kwargs
#1
75
If you want to use them like that, define the function with the variable names as normal (but use klass
for class
, you can't use reserved words):
如果你想这样使用它们,用变量名来定义函数(但是使用klass作为类,你不能使用保留字):
def my_function(school, city, klass, name):
schoolname = school
cityname = city
standard = klass
studentname = name
Now (as long as you rename class to klass in your dictionary) you can use **
when you call the function:
现在(只要在字典中将类重命名为klass),就可以在调用函数时使用**:
my_function(**data)
and it will work as you want.
你想怎么做就怎么做。
#2
28
*data interprets arguments as tuples, instead you have to pass **data which interprets the arguments as dictionary.
*数据将参数解释为元组,相反,您必须传递**数据,该数据将参数解释为字典。
data = {'school':'DAV', 'class': '7', 'name': 'abc', 'city': 'pune'}
def my_function(**data):
schoolname = data['school']
cityname = data['city']
standard = data['class']
studentname = data['name']
You can call the function like this:
你可以这样调用函数:
my_function(**data)
#3
6
You can just pass it
你可以通过它
def my_function(my_data):
my_data["schoolname"] = "something"
print my_data
or if you really want to
或者如果你真的想
def my_function(**kwargs):
kwargs["schoolname"] = "something"
print kwargs