Python 为什么我会得到这个错误属性错误:';str';对象没有属性';获取';get函数在字典中的什么位置?
我有一本字典:Python 为什么我会得到这个错误属性错误:';str';对象没有属性';获取';get函数在字典中的什么位置?,python,dictionary,Python,Dictionary,我有一本字典: fruits = { "apple": ["red", "juicy"] } 这个字典在许多函数中传递和返回。我不知道为什么,但在这行代码中,我得到了一个AttributeError def get_dict(dict): dictionary = dict appels_attributes = dictionary.get("apple") return apples_a
fruits = {
"apple": ["red", "juicy"]
}
这个字典在许多函数中传递和返回。我不知道为什么,但在这行代码中,我得到了一个AttributeError
def get_dict(dict):
dictionary = dict
appels_attributes = dictionary.get("apple")
return apples_attributes
print(get_dict(fruits))
是否有什么地方出错或我遗漏了什么?首先,您需要检查语法。
在代码中:
def get_dict(dict):
dictionary = dict
appels_attributes = dictionary.get("apple")
# appels and apples are not equal.
return apples_attributes
我想你犯了一些拼写错误。
第3行:appels
第四行:苹果
所以只定义了一个变量。这是“appels_attributes”,您返回了未定义的“apples.attributes”。
这就是为什么会出现属性错误
而且也不要使用python预定义的任何保留语法,如“dict”。一旦您删除输入错误“appels\u attributes”/“apples\u attributes”,它就会正常工作。请提供一份报告。当它被传递到这个函数时,fruits
可能不再是您认为的那样。请不要将您的变量命名为pythonbuiltin
s。为您的dict
使用其他名称。