将代码放入函数时出现"Local variable referenced before assignment "

"Local variable referenced before assignment " appears when putting code into function

在尝试将代码放入函数之前,我的代码工作正常。定义函数、缩进代码并调用函数后,我收到错误消息:

"Local variable 'print' referenced before assignment.

为什么当我所做的只是将它放入函数中时弹出?我从不分配变量 'print.' 请帮忙!
为了安全起见,我屏蔽了访问服务器的令牌。

    def printSet():
        for user in range (0,len(parsed_json['members'])-1):
            userDict=parsed_json['members'][user]#Catches errors resulting from users not having all settings configured
            try:
                print("id: "+userDict["id"])
            except KeyError:
                print("No ID found")
            try:
                print("team id: "+userDict["team_id"])
            except KeyError:
                print("No team ID found")
            try:
                print("name: "+userDict["name"])
            except KeyError:
                print("No name found")
            try:
                print("real name: "+userDict["real_name"])
            except KeyError:
                print("No real name found")
            userProf=userDict['profile']
            try:
                print("title: "+userProf["title"])
            except KeyError:
                print("No title found")
            try:
                print("real name: "+userProf["real_name"])
            except KeyError:
                print("No real name found")
            try:
                print("real name normalized: "+userProf["real_name_normalized"])
            except KeyError:
                print("No real name normalized found")
            try:
                print("display name: "+userProf["display_name"])
            except KeyError:
                print("No display name found")
            try:
                print("display name normalized: "+userProf["display_name_normalized"])
            except KeyError:
                 print("No display name normalized found")
            try:
                print("email: "+userProf["email"])
            except KeyError:
                print:("No email found")
            try:
                print("first name: "+userProf["first_name"])
            except KeyError:
                print("No first name found")
            try:
                print("last name: "+userProf["last_name"])
            except KeyError:
                print("No last name found")
    #To easily show when one member ends and another begins
            print("----------------------------------")
    printSet()

"I assign parsed_json elsewhere in my code"

嗯,这就是问题所在。您必须将变量传递给代码,或将其声明为全局变量,但最好避免这种情况。您可以像这样进行简单的更改。

def printSet(parsed_json):
    for user in range (0,len(parsed_json['members'])-1):
       userDict=parsed_json['members'][user]
    #####do a bunch of stuff or whatever

printSet(parsed_json)

请注意,您需要为函数中使用的每个变量执行此操作

其他可以简化您的代码并帮助调试的方法是摆脱所有的 try-excepts

keylist=["id", "real_name", ..... "last_name"] #not required, but helpful if you want to print "not found" type messages
for k in keylist:
    if k in userDict.keys():
        print('{}: {}'.format(k, userDict[k]))
    else:
        print('No {} found'.format(k))

I never assign a variable 'print.'

是的,你这样做:

print:("No email found")

这是一个 annotated assignment statement,它用类型 "No email found" 注释 print,但不分配任何值。

带注释的赋值总是创建一个局部变量,即使您没有赋值。来自文档:

If a name is annotated in a function scope, then this name is local for that scope.

如果您想知道,空注释赋值对这种情况很有用:

n: int
if spam:
    n = spam**2
else:
    n = -1

这是唯一可以告诉像 Mypy 这样的静态类型检查器来验证 n 最终持有 int 的唯一方法,无论您选择哪个 if 分支。