Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使局部变量成为全局变量的步骤_Python 3.x - Fatal编程技术网

Python 3.x 使局部变量成为全局变量的步骤

Python 3.x 使局部变量成为全局变量的步骤,python-3.x,Python 3.x,我试图找出如何使局部函数变量成为全局变量,以便在其他函数中使用它 def question(): ask = input("Are you OK?:").lower() def check_question(n): if ask != 'yes': question() else: return ask m = check_question(ask) print

我试图找出如何使局部函数变量成为全局变量,以便在其他函数中使用它

def question():
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m
question()


print(f'he said:  {m}')

def what():
    print(m)
这里不需要全局变量

函数返回一个值--您可以使用该值,如下所示:

m = question()
print(f'he said: {m}')
def what(m):
    print(m)
what(m)
m = ''
def question():
    global m
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m question()


print(f'he said:  {m}')

def what():
    print(m)
对于其他函数
what
,您可以在调用时向其传递输入参数,例如,如下定义:

m = question()
print(f'he said: {m}')
def what(m):
    print(m)
what(m)
m = ''
def question():
    global m
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m question()


print(f'he said:  {m}')

def what():
    print(m)
这样称呼它:

m = question()
print(f'he said: {m}')
def what(m):
    print(m)
what(m)
m = ''
def question():
    global m
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m question()


print(f'he said:  {m}')

def what():
    print(m)

一般来说,当问题可以通过使用函数参数和返回值轻松解决时,请避免使用全局变量。

要回答您的问题,在函数中,您必须先将变量声明为全局变量,然后才能按如下方式为其赋值:

m = question()
print(f'he said: {m}')
def what(m):
    print(m)
what(m)
m = ''
def question():
    global m
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m question()


print(f'he said:  {m}')

def what():
    print(m)

但正如科斯塔帕拉斯所指出的,有比使用全局变量更好的方法来实现这一点。

您只需执行
global m

def question():
    global m
    ask = input("Are you OK?:").lower()

    def check_question(n):
        if ask != 'yes':
            question()
        else:
            return ask

    m = check_question(ask)
    print (m)
    return m
question()


print(f'he said:  {m}')

def what():
    print(m)

关键是我不想调用question(),我需要得到返回的m并使其全局化,以便能够在其他函数中使用。这真的没有意义。您的
question()。从函数返回值的点(这是您已经在做的事情)是这样,它可以在其他地方使用——包括按照我的建议将其传递给其他函数。在函数外部创建变量,使其在python中成为全局变量您正在询问变量
m
?不要使用可变全局变量您已经
返回m
,那么为什么不使用它呢?