简体   繁体   English

如何将字典分配给另一个字典?

[英]How to assign dictionary to another dictionary?

So I want to create a function that accepts values for dictionary, creates new dictionary and returns it.所以我想创建一个 function 接受字典值,创建新字典并返回它。 Then I want to assign that value in other places of code.然后我想在其他代码位置分配该值。 But I get Type Error dict object is not callable.但我得到 Type Error dict object is not callable。 How can I do this?我怎样才能做到这一点?

def shoe(brand, model):
    shoe = {}
    shoe['brand'] = brand
    shoe['model'] = model
    return shoe


 shoes_list = []
 shoe = {}

 shoe = shoe('Some Brand', 'Some model')

 print(shoe)
def shoe(brand, model):

Here you create a function named shoe .在这里,您创建了一个名为shoe的 function 。

    shoe = {}

Now you create a variable named shoe and assign a dictionary to it.现在您创建一个名为shoe的变量并为其分配一个字典。 This isn't a problem yet since this variable is local inside of the function.这还不是问题,因为这个变量是 function 内部的本地变量。 However, it can be very confusing to reuse the same name in this way.但是,以这种方式重用相同的名称可能会非常混乱。 I suggest changing this variable's name.我建议更改此变量的名称。

    shoe['brand'] = brand
    shoe['model'] = model
    return shoe


 shoes_list = []
 shoe = {}

Now you are reassigning the name shoe to refer to an empty dictionary instead of the function that it used to refer to.现在您正在重新分配名称shoe以引用一个空字典,而不是它过去引用的 function。 This will cause later calls to the function with shoe() to fail.这将导致以后使用shoe()调用 function 失败。


 shoe = shoe('Some Brand', 'Some model')

And then you try to assign the return value of the function to the name shoe() .然后您尝试将 function 的返回值分配给名称shoe() I assume this is where the function call fails.我认为这是 function 调用失败的地方。 Note that if you fix this, you don't need shoe = {} at all.请注意,如果您解决此问题,则根本不需要shoe = {}


 print(shoe)

You are using the same name for two different things: once for the name of your function and once for the name of your dictionary.您对两个不同的事物使用相同的名称:一次用于您的 function 的名称,一次用于您的字典名称。 Use two different names to fix the problem.使用两个不同的名称来解决问题。 In general, function names should be verbs.通常,function 名称应该是动词。 So you can do get_shoe() or create_shoe() for the function name.因此,您可以为 function 名称执行get_shoe()create_shoe() Then using the noun shoe for the dictionary will be fine.然后将名词shoe用于字典就可以了。

You need your function and your original dict to have different names:您需要您的 function 和原始字典具有不同的名称:

def make_shoe(brand, model):
    shoe = {}
    shoe['brand'] = brand
    shoe['model'] = model
    return shoe


 shoes_list = []
 shoe = {}

 shoe = make_shoe('Some Brand', 'Some model')

 print(shoe)

Note also that you are not filling in the dict you stored in shoe ;另请注意,您没有填写您存储在shoe中的 dict ; you are making a new dict to replace it.你正在制作一个新的字典来替换它。 If you want to fill it in, you would do something like this:如果你想填写它,你会做这样的事情:

def fill_in(brand, model, dest):
    dest['brand'] = brand
    dest['model'] = model


 shoes_list = []
 shoe = {}

 fill_in('Some Brand', 'Some model', shoe)

 print(shoe)

You've shadowed the function named shoe by assigning the name shoe to refer an empty dict:您通过将名称shoe指定为引用一个空字典来隐藏名为shoe的 function :

shoe = {}

And in the very next line, you're trying to call shoe as a function -- which now refers to the empty dict , not the earlier function you defined.在下一行中,您尝试将shoe称为 function - 现在指的是空dict ,而不是您定义的早期 function 。

So pick a different name for either one.因此,为其中任何一个选择不同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM