나는 하나 이상의 다른 함수 내에서 (비 로컬 변수를 사용하여) 파이썬 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)
클래스가 필요합니다. http://docs.python.org/tutorial/classes.html – Crisfole
[ '로컬이 아닌 것'에 대한 설명] (http://stackoverflow.com/a/) 1261961/832391). –
간단히 대답하면 역동적 범위 지정을 요구하고 있지만 파이썬 ('로컬이 아닌 경우와없는')은 렉시 컬 범위 지정 만 제공합니다. – delnan