gpt4 book ai didi

python - Python 中内部函数的重用

转载 作者:行者123 更新时间:2023-11-30 23:47:05 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何在多个其他函数中使用 Python 3 函数(使用非局部变量),而无需重新定义它。这是一个非常简单的例子来说明我的意思:

def inner(airplane):
nonlocal var
if airplane == "Alpha":
var = a
elif airplane == "Beta":
var = b
def outer1(airplane):
inner(airplane)
do stuff with var
def outer2(airplane)
inner(airplane)
do other stuff with var

outer1("FirstAirplane")
outer2("SecondAirplane")

我收到错误( SyntaxError: No binding for nonlocal 'var' found ),但我怀疑我做错了。我从来不打算运行inner()在其自己的。如何重复使用inner()正确的方式?我不能只在 outer1() 内定义它然后在 outer2() 中重复使用它,可以吗?

好吧,呃...大众需求...这是我的代码的相关部分...

def planeandoffset(airplane):
if airplane == "Zulu":
linename = "\\[\\INOV"
charoffset = 14
elif airplane == "Lima":
linename = "\\[\\ILIM"
charoffset = 10
elif airplane == "Mike":
linename = "\\[\\IMIK"
charoffset = 10
else:
print("There is no airplane by that name.")
latstart = charoffset
latend = 7 + charoffset
lonstart = 9 + charoffset
lonend = 17 + charoffset
return airplane, linename, latstart, latend, lonstart, lonend

def latlongen(workingline, latstart, latend, lonstart, lonend):
# Determine Latitude and Longitude in decimal format
latraw = workingline[latstart:latend]
if latraw[0:1] == "S":
pm = "-"
else:
pm = ""
hours = float(latraw[3:5] + "." + latraw[5:])
decimal = hours/60
latitude = float(latraw[1:3]) + decimal
latitude = float(pm + str(latitude))
lonraw = workingline[lonstart:lonend]
if lonraw[0:1] == "W":
pm = "-"
else:
pm = ""
hours = float(lonraw[4:6] + "." + lonraw[6:])
decimal = hours/60
longitude = float(lonraw[1:4]) + decimal
longitude = float(pm + str(longitude))
return latitude, longitude
def kmlplanegen(airplane):
planeandoffset(airplane)
global afffilename, iconurl, kmlwrite
affread = open(afffilename)
while True:
line = affread.readline()
# Choose appropriate line
if line.startswith(linename):
workingline = line
elif len(line) == 0: # Zero length indicates EOF (Always)
break
else:
pass
try:
latlongen(workingline, latstart, latend, lonstart, lonend)
# Generate kml for Airplane
print(''' <Placemark>
<Style>
<IconStyle>
<Icon>
<href>{0}</href>
</Icon>
</IconStyle>
</Style>
<name>{1}</name>
<description>Latitude: {2} Longitude: {3}</description>
<Point>
<coordinates>{3},{2},0</coordinates>
</Point>
</Placemark>'''.format(iconurl,airplane,latitude,longitude), file=kmlwrite)
except Exception:
exit(1, "There was an error. This message is kind of worthless. Stopping Program")

def kmlpathgen(airplane):
planeandoffset(airplane)
global afffilename, kmlwrite
# Generate kml for Airplane Path
print(''' <Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark>
<name>{0} Path</name>
<description>Transparent green wall with yellow outlines</description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates>'''.format(airplane), file=kmlwrite)
try:
affread = open(afffilename)
while True:
line = affread.readline()
if len(line) == 0: # Zero length indicates EOF (Always)
break
elif line.startswith(linename):
workingline = line
latlongen(workingline, latstart, latend, lonstart, lonend)
print(" {0},{1},0".format(longitude,latitude), file=kmlwrite)
else:
pass
except NameError:
pass
finally:
print(''' </coordinates>
</LineString>
</Placemark>''', file=kmlwrite)

最佳答案

您刚刚学习编程吗?

忘记非本地的任何事情。正确的方法是使用 return var 返回 var,然后在调用上下文中分配其结果:

var = inner(airplane)

编辑:

您只是打电话的地方:

planeandoffset(airplane)

您没有对返回的值执行任何操作。要访问它们,请执行以下操作:

airplane, linename, latstart, latend, lonstart, lonend = planeandoffset(airplane)

我不会分析你对这些全局变量做了什么,但不管它是什么,停止吧。要么将它们作为参数在函数之间传递,要么将它们与函数一起放在一个类中,并作为类的成员访问它们。

无论哪种方式,我建议您按照教程学习一种不使用全局变量的更常见的编程风格。

关于python - Python 中内部函数的重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8496011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com