python函数之间变量的调用

1.实现一个函数fun1()和函数fun2()之间变量的调用,通常使用返回值的方法,在调用的时候,会将调用的函数整体运行一遍。

def fun1(x,y):
    print("This is fun1")
    test = x + y
    return test

def fun2():
    test2 = fun1(1,3)
    print(test2)

2.取fun1()中的值不运行fun1()的函数体,需要把所取的值设置为全局变量,全局变量必须先定义,然后在函数中声明并使用,这样就可以在fun2()中调用

test1 = 10 #定义一个全局变量
def fun1():
    print("This is fun1")
    global test1 #声明该处使用的是全局变量
    test1 = 100
    return test1

def fun2():
    print("This is fun2")
    global test1
    print(test1)

输出 100

你可能感兴趣的:(python)