概述
码:
def user(repo, language, page):
# import pdb;pdb.set_trace()
dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'watchers': repo['watchers'], 'username': user['login'], 'type': user['type'], 'public_repos': user['public_repos'], 'followers': user['followers']}
fields_user = ['blog', 'company', 'location', 'name']
fields_repo = ['description']
if user.get('email'):
dictionary_2 = {'email': user['email']}
for key in fields_user:
if user.has_key(key):
dictionary[key] = user[key]
for key in fields_repo:
if repo.has_key(key):
dictionary[key] = repo[key]
shelf[dictionary_2['email']] = dictionary
shelf.sync()
return dictionary_2['email']
print dictionary_2['email']
由于某些原因,我不断收到此错误:
in user(repo, language, page)
1 def user(repo, language, page): #returns email address to pass into rapportive
2 # import pdb;pdb.set_trace()
----> 3 dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'followers': user['followers']}
4 # dictionary = {, , 'watchers': repo['watchers'], 'username': user['login']} , 'type': user['type'], , 'public_repos': user['public_repos']
5 fields_user = ['blog', 'company', 'location', 'name']
TypeError: 'function' object is not subscriptable
而且我不明白为什么。我只想写更多的key:value对字典。但是由于某种原因,这总是会引发该错误。
解决方案
user是一个功能:
def user(repo, language, page):
但是在该函数中,您尝试将其用作字典:
user['followers']
在您定义的行上dictionary:
dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'followers': user['followers']}
您可以将该行划分为几行,以使其更具可读性和可调试性:
dictionary = {
'language_search': language,
'page': page,
'followers': repo['followers'],
'github_repo': repo['name'],
'forked?': repo['fork'],
'forks': repo['forks'],
'github_owner': repo['owner'],
'language': repo['language'],
'created_at': repo['created_at'],
'forks': repo['forks'],
'followers': user['followers'] # `user` is a function, what did you mean instead?
}
您似乎希望user整个函数中都有字典:
if user.get('email'): # Will also throw the error
dictionary_2 = {'email': user['email']} # as will this
for key in fields_user:
if user.has_key(key): # and this
dictionary[key] = user[key] # and here
也许您有一个名为user字典的全局名称?模块中不能同时具有字典和具有相同名称的函数。重命名一个或另一个。
最后
以上就是温柔镜子为你收集整理的python中属性对象不可下标是什么意思_“'功能'对象不可下标”的全部内容,希望文章能够帮你解决python中属性对象不可下标是什么意思_“'功能'对象不可下标”所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复