Python-每个函数运行时使用不同的变量
我会尽力解释清楚的。我创建了一个函数,它使用Tkinter的tkFileDialog.askopenfilename()来选择照片。脚本返回时带有两个变量中的坐标:Python-每个函数运行时使用不同的变量,python,function,variables,unique,Python,Function,Variables,Unique,我会尽力解释清楚的。我创建了一个函数,它使用Tkinter的tkFileDialog.askopenfilename()来选择照片。脚本返回时带有两个变量中的坐标: def getCoordinates(): # code to get coordinates here # variables for lat and long global latitude global longitude latitude longitude 现在,我创
def getCoordinates():
# code to get coordinates here
# variables for lat and long
global latitude
global longitude
latitude
longitude
现在,我创建了一个函数,它两次调用第一个函数getCoordinates()。因此,这需要选择两张照片
def TwoPic():
run = 0
while run < 2:
run = run + 1
getCoordinates()
FirstRunLat = latitude
FirstRunLong = longitude
print FirstRunLat + " " + FirstRunLong
SecRunLat = latitude
SecRunLong = longitude
print SecRunLat + " " + SecRunLong
ThirdRunLat = latitude
ThirdRunLong = longitude
print ThirdRunLat + " " + ThirdRunLong
root.quit()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
getCoordinates()
FirstRunLat=纬度
FirstRunLong=经度
打印FirstRunLat+“”+FirstRunLong
SecRunLat=纬度
SecRunLong=经度
打印SecRunLat+“”+SecRunLong
第三纬度=纬度
ThirdRunLong=经度
打印ThirdRunLat+“”+Thirdrunlot
root.quit()
基本上会发生的是,SecRunLat和SecRunLong以及ThirdRunLat和ThirdRunLong最终将与FirstRunLat和FirstRunLong相同
所以我要问的是,如果我再次运行这个函数,我如何运行这个函数来获得坐标,并给变量赋予不同的名称,这些名称保持唯一,并且不会重复
希望这是有意义的
事先非常感谢
基本上会发生的是,SecRunLat
&SecRunLong
和ThirdRunLat
&ThirdRunLong
最终将与FirstRunLat
和FirstRunLong
相同
当然-您不会在这两者之间更改值,因为您只调用了一次getCoordinates()
最好返回您的结果:
def getCoordinates():
# code to get coordinates here
return latitude, longitude # as a tuple.
def TwoPic():
run = 0
results = []
while run < 2:
run = run + 1
lat, lng = getCoordinates()
print lat + " " + lng
results.append((lat, lng))
print results
root.quit()
def getCoordinates():
#在这里获取坐标的代码
以元组形式返回纬度、经度。
def TwoPic():
运行=0
结果=[]
运行<2时:
运行=运行+1
纬度,lng=getCoordinates()
打印纬度+“”+lng
结果。追加((lat,lng))
打印结果
root.quit()
基本上会发生的是,SecRunLat
&SecRunLong
和ThirdRunLat
&ThirdRunLong
最终将与FirstRunLat
和FirstRunLong
相同
当然-您不会在这两者之间更改值,因为您只调用了一次getCoordinates()
最好返回您的结果:
def getCoordinates():
# code to get coordinates here
return latitude, longitude # as a tuple.
def TwoPic():
run = 0
results = []
while run < 2:
run = run + 1
lat, lng = getCoordinates()
print lat + " " + lng
results.append((lat, lng))
print results
root.quit()
def getCoordinates():
#在这里获取坐标的代码
以元组形式返回纬度、经度。
def TwoPic():
运行=0
结果=[]
运行<2时:
运行=运行+1
纬度,lng=getCoordinates()
打印纬度+“”+lng
结果。追加((lat,lng))
打印结果
root.quit()
好吧,如果你想继续使用globals,我只会使用列表:
def TwoPic():
run = 0
Lats = []
Longs = []
while run < 2:
run = run + 1
getCoordinates()
Lats.append(latitude)
Longs.append(longitude)
root.quit()
def TwoPic():
运行=0
Lats=[]
Longs=[]
运行<2时:
运行=运行+1
getCoordinates()
纬度附加(纬度)
Longs.append(经度)
root.quit()
好吧,如果你想继续使用globals,我只会使用列表:
def TwoPic():
run = 0
Lats = []
Longs = []
while run < 2:
run = run + 1
getCoordinates()
Lats.append(latitude)
Longs.append(longitude)
root.quit()
def TwoPic():
运行=0
Lats=[]
Longs=[]
运行<2时:
运行=运行+1
getCoordinates()
纬度附加(纬度)
Longs.append(经度)
root.quit()
从函数返回值,并将其作为元组分配:
def getCoordinates():
# code to get coordinates here
return latitude, longitude
在调用这些时:
def TwoPic():
run = 0
while run < 2:
run = run + 1
FirstRunLat, FirstRunLong = getCoordinates()
print FirstRunLat + " " + FirstRunLong
SecRunLat, SecRunLong = getCoordinates()
print SecRunLat + " " + SecRunLong
ThirdRunLat, ThirdRunLong = getCoordinates()
print ThirdRunLat + " " + ThirdRunLong
root.quit()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
FirstRunLat,FirstRunLong=getCoordinates()
打印FirstRunLat+“”+FirstRunLong
SecRunLat,SecRunLong=getCoordinates()
打印SecRunLat+“”+SecRunLong
ThirdRunLat,ThirdRunLong=getCoordinates()
打印ThirdRunLat+“”+Thirdrunlot
root.quit()
不要使用全局函数传递函数结果;这就是return
语句的作用。从函数中返回值,并将其指定为元组:
def getCoordinates():
# code to get coordinates here
return latitude, longitude
在调用这些时:
def TwoPic():
run = 0
while run < 2:
run = run + 1
FirstRunLat, FirstRunLong = getCoordinates()
print FirstRunLat + " " + FirstRunLong
SecRunLat, SecRunLong = getCoordinates()
print SecRunLat + " " + SecRunLong
ThirdRunLat, ThirdRunLong = getCoordinates()
print ThirdRunLat + " " + ThirdRunLong
root.quit()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
FirstRunLat,FirstRunLong=getCoordinates()
打印FirstRunLat+“”+FirstRunLong
SecRunLat,SecRunLong=getCoordinates()
打印SecRunLat+“”+SecRunLong
ThirdRunLat,ThirdRunLong=getCoordinates()
打印ThirdRunLat+“”+Thirdrunlot
root.quit()
不要使用全局函数传递函数结果;这就是
return
语句的目的。这是一种非常糟糕的方法。要么使用列表或字典来存储FirstRunLat
和其他内容,要么只进行函数调用而不进行循环。这是一种非常糟糕的方法。可以使用列表或字典来存储FirstRunLat
和其他内容,也可以不使用循环来执行函数调用。