Python中的全局变量与局部变量2

from route_design1217 import *
from math import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
v_front = 0
d = 12

def frenet_to_global(current_x, current_y, heading, theta, a):  # transform from frenet coord to global coord
    T = np.array([[current_x], [current_y]])
    heading = -heading * pi / 180
    # print(heading)
    R = np.array([[cos(heading), -sin(heading)], [sin(heading), cos(heading)]])
    trans1 = np.hstack((R, T))
    rotation2 = np.vstack((trans1, np.array([0, 0, 1])))
    T = np.array([[0], [0]])
    R = np.array([[cos(pi / 2 - theta), -sin(pi / 2 - theta)], [sin(pi / 2 - theta), cos(pi / 2 - theta)]])
    trans1 = np.hstack((R, T))
    rotation1 = np.vstack((trans1, np.array([0, 0, 1])))
    a = np.hstack((a[:, [0, 1]], np.ones((len(a), 1)))).T
    trans = np.dot(rotation2, np.dot(rotation1, a)).T
    trans = trans[:, [0, 1]]
    return trans  # return a n by 2 transfomation matrix
#end function

a = np.random.rand(10,2)
print('matrix a is ',a)
current_x = 0
current_y = 0
heading = 60 * pi/180
theta = 2*pi/180
trans = frenet_to_global(current_x, current_y, heading, theta, a)
# print('matrix trans is',trans)
####################################################################
#对于函数嵌套函数的情况,我们定义一些全局变量
value = 1
add = 1
minus = -2
target = 2
value2 = 13
# 主函数为update_value, 内部还嵌套了update2_value
# update2_value函数input了value值,return了被修改的value值
def update2_value(value):
    #注意,这里我们没有引入global value这个量,因为函数已经把他作为输入带入了,不允许再次定义全局变量
    value = value * 2 + value2
    return value
#update_value函数引入全局变量value值,再调用update2_value函数计算一个新的value并return
def update_value():
    global value
    value = update2_value(value) + target
    return value

for i in range(1,3):
    value = update_value()
    print('the updated value is',value)
######################################################
value = 1
add = 1
minus = -2
target = 2
value2 = 13

def update2_value():
    #update2_value的另一种写法,不在函数中引入global value 变量,而是通过单独声明global value命令引入value
    # 两种写法,最后可以return或者不return,这个只会影响如何取值,如果return了,直接使用 value = 。。。即可
    # 如果不return你就需要先运行一次这个函数,更新value值,然后在对value做其他操作,这里是return的写法
    global value
    value = value * 2 +  value2
    print('in value2 func, value is:',value)
    return value


def update_value():
    global value
    value = update2_value() + target


for i in range(1,3):
    update_value()
    print('the updated in second trial value is',value)
######################################################

value = 1
add = 1
minus = -2
target = 2
value2 = 13

def update2_value():
    #update2_value的另一种写法,不在函数中引入global value 变量,而是通过单独声明global value命令引入value
    # 两种写法,最后可以return或者不return,这个只会影响如何取值,如果return了,直接使用 value = 。。。即可
    # 如果不return你就需要先运行一次这个函数,更新value值,然后在对value做其他操作。这里是不return的写法
    global value #这个global是要对本函数下的value进行更新,是必要的
    value = value * 2 +  value2
    print('in value2 func, value is:',value)



def update_value():
    global value#这个global是对本函数下的global进行更新,是必要的的
    update2_value()# 可以看到,这个函数里面先运行了一次update2_value函数,更新了value值
    value = value + target


for i in range(1,3):
    update_value()
    print('the updated in second trial value is',value)
发布了56 篇原创文章 · 获赞 11 · 访问量 8755

猜你喜欢

转载自blog.csdn.net/gophae/article/details/103905598