在Python中,如何从函数对象外部引用函数对象中定义的变量?
我希望能够使在latitude()函数对象内定义的变量(如dns_float_a)在latitude()之外工作 下面是我正在使用的代码部分。它显示了使用def()定义的两个变量对象:。然后我想在底部打印等式,但为了实现这一点,我需要引用函数对象中的变量 PS这是我写的第一个程序(这是一个较长的纬度/经度转换程序的一部分),所以请不要以为我知道的太多 提前谢谢在Python中,如何从函数对象外部引用函数对象中定义的变量?,python,function,variables,object,Python,Function,Variables,Object,我希望能够使在latitude()函数对象内定义的变量(如dns_float_a)在latitude()之外工作 下面是我正在使用的代码部分。它显示了使用def()定义的两个变量对象:。然后我想在底部打印等式,但为了实现这一点,我需要引用函数对象中的变量 PS这是我写的第一个程序(这是一个较长的纬度/经度转换程序的一部分),所以请不要以为我知道的太多 提前谢谢 def latitude(): #LATITUDE print "***Degrees/Minutes/Seconds
def latitude():
#LATITUDE
print "***Degrees/Minutes/Seconds >>> Decimal Degrees***"
print
print "Input Latitude"
dns_a=raw_input("Degrees: ")
mns_a=raw_input("Minutes: ")
sns_a=raw_input("Seconds: ")
ns=raw_input("North (1) or South (2): ")
dns_float_a=float(dns_a)
mns_float_a=float(mns_a)
sns_float_a=float(sns_a)
ns_float=float(ns)
#south
if ns_float==2:
dns_float_a=dns_float_a*(-1)
mns_float_a=mns_float_a*(-1)
sns_float_a=sns_float_a*(-1)
ns_x="South"
#north
elif ns_float==1:
dns_float_a=dns_float_a*1
mns_float_a=mns_float_a*1
sns_float_a=sns_float_a*1
ns_x="North"
elif ns_float<1 or ns_float>2 or ns_float>1 and ns_float<2:
print
print "*Invalid Input*"
latitude()
def longitude():
#LONGITUDE
print
print "Input Longitude"
dns_b=raw_input("Degrees: ")
mns_b=raw_input("Minutes: ")
sns_b=raw_input("Seconds: ")
ns=raw_input("East (1) or West (2): ")
dns_float_b=float(dns_b)
mns_float_b=float(mns_b)
sns_float_b=float(sns_b)
ns_float=float(ns)
#south
if ns_float==2:
dns_float_b=dns_float_b*(-1)
mns_float_b=mns_float_b*(-1)
sns_float_b=sns_float_b*(-1)
ns_x="South"
#north
elif ns_float==1:
dns_float_b=dns_float_b*1
mns_float_b=mns_float_b*1
sns_float_b=sns_float_b*1
ns_x="North"
elif ns_float<1 or ns_float>2 or ns_float>1 and ns_float<2:
print
print "*Invalid Input*"
longitude()
latitude()
longitude()
#(d/m/s)ns_float_a
decimal_degrees_latitude=(dns_float_a)+(mns_float_a/60)+(sns_float_a/3600)
#(d/m/s)ns_float_b
decimal_degrees_longitude=(dns_float_b)+(mns_float_b/60)+(sns_float_b/3600)
print
print "Results:"
print
print "Latitude: ", decimal_degrees_latitude
print "Longitude: ", decimal_degrees_longitude
print
def latitude():
#纬度
打印“***度/分/秒>>>十进制***”
打印
打印“输入纬度”
dns_a=原始_输入(“度:”)
mns\u a=原始输入(“分钟:”)
sns\u a=原始输入(“秒:”)
ns=原始输入(“北(1)或南(2):”)
dns_float_a=浮动(dns_a)
mns\U浮动\U a=浮动(mns\U a)
sns\U浮动=浮动(sns\U a)
ns_浮点=浮点(ns)
#南方
如果ns_float==2:
dns_float_a=dns_float_a*(-1)
mns\U浮点值\U a=mns\U浮点值\U a*(-1)
sns\U浮点值\U a=sns\U浮点值\U a*(-1)
ns_x=“南部”
#北
elif ns_float==1:
dns_float_a=dns_float_a*1
mns\U浮动\U a=mns\U浮动\U a*1
sns_float_a=sns_float_a*1
ns_x=“北”
elif ns_float2或ns_float>1和ns_float1以及ns_float从函数返回值,并在末尾写入:
return dns_float_a, mns_float_a, sns_float_a
并在室外使用:
dns, mns, sns = latitude()
从函数返回值,在末尾写入:
return dns_float_a, mns_float_a, sns_float_a
并在室外使用:
dns, mns, sns = latitude()
这是Python内部的一个问题。基本上,您不能访问函数外部的函数内部定义的变量
解决这个问题的一种方法是将所需的变量作为返回类型从函数中传递。这实际上可以简化代码,因为每个函数中的所有变量都不需要_a和_b。作用域规则意味着您可以在两个不同的函数中使用相同名称的变量,并且它们不会相互重写。在纬度和经度函数的末尾添加以下行:
return dns_float,mns_float,sns_float
然后将函数调用更改为:
dns_float_a,mns_float_a,sns_float_a = latitude()
dns_float_b,mns_float_b,sns_float_b = latitude()
decimal_degrees_latitude = latitude()
decimal_degrees_longitude = longitude()
或者,您可以将纬度和经度计算移到函数本身中。将每个函数的最后一行更改为:
return dns_float+mns_float/60+sns_float/3600
然后将函数调用更改为:
dns_float_a,mns_float_a,sns_float_a = latitude()
dns_float_b,mns_float_b,sns_float_b = latitude()
decimal_degrees_latitude = latitude()
decimal_degrees_longitude = longitude()
这是Python内部的一个问题。基本上,您不能访问函数外部的函数内部定义的变量
解决这个问题的一种方法是将所需的变量作为返回类型从函数中传递。这实际上可以简化代码,因为每个函数中的所有变量都不需要_a和_b。作用域规则意味着您可以在两个不同的函数中使用相同名称的变量,并且它们不会相互重写。在纬度和经度函数的末尾添加以下行:
return dns_float,mns_float,sns_float
然后将函数调用更改为:
dns_float_a,mns_float_a,sns_float_a = latitude()
dns_float_b,mns_float_b,sns_float_b = latitude()
decimal_degrees_latitude = latitude()
decimal_degrees_longitude = longitude()
或者,您可以将纬度和经度计算移到函数本身中。将每个函数的最后一行更改为:
return dns_float+mns_float/60+sns_float/3600
然后将函数调用更改为:
dns_float_a,mns_float_a,sns_float_a = latitude()
dns_float_b,mns_float_b,sns_float_b = latitude()
decimal_degrees_latitude = latitude()
decimal_degrees_longitude = longitude()
您已经被告知如何使函数内部的数据在外部可用
但您可以做得更多:由于这两个函数几乎相同,您可以将它们中的大多数组合成一个通用函数
class InvalidInputError(ValueError): pass
def enter_degrees(input_what, pos_string, neg_string):
print
print "Input " + input_what
d_a = int(raw_input("Degrees: "))
m_a = int(raw_input("Minutes: "))
s_a = int(raw_input("Seconds: "))
dir = int(raw_input(pos_string + " (1) or " + neg_string + " (2)"))
# south or west - negative:
if dir == 2:
d_a = -d_a
m_a = -m_a
s_a = -s_a
dir_x = neg_string
# north or east - positive
elif ns_float==1:
dir_x = pos_string
else:
raise InvalidInputError("1 or 2!")
# return d_a, m_a, s_a, dir_x
return d_a + m_a / 60.0 + s_a / 3600.0
def enter_degrees_loop(input_what, pos_string, neg_string):
while True:
try:
return enter_degrees(input_what, pos_string, neg_string)
except InvalidInputError, e:
print e # output the error
continue
def enter_latitude():
return enter_degrees_loop("Latitude", "North", "South")
def enter_longitude():
return enter_degrees_loop("Longitude", "East", "West")
print "***Degrees/Minutes/Seconds >>> Decimal Degrees***"
print
latitude = enter_latitude()
longitude = enter_longitude()
print
print "Results:"
print
print "Latitude: ", latitude
print "Longitude: ", longitude
print
如您所见,我做了一些额外的更改:
- 我将转换从
float()
更改为int()
:在这里使用非整数没有任何意义
- 我已将
打印语句放在它们所属的位置
- 我使用一个异常来指示输入错误,并将另一个层环绕该all循环,直到正确输入all
- 我将
dua*(-1)
替换为-dua
,dua*1
替换为dua
- 我将计算移到最里面的函数中
仍然可以做的事情:
- 检查度数、分和秒的范围是否正确
- 省略不需要的内容(例如
dir\ux
)
您已经被告知如何使函数内部的数据在外部可用
但您可以做得更多:由于这两个函数几乎相同,您可以将它们中的大多数组合成一个通用函数
class InvalidInputError(ValueError): pass
def enter_degrees(input_what, pos_string, neg_string):
print
print "Input " + input_what
d_a = int(raw_input("Degrees: "))
m_a = int(raw_input("Minutes: "))
s_a = int(raw_input("Seconds: "))
dir = int(raw_input(pos_string + " (1) or " + neg_string + " (2)"))
# south or west - negative:
if dir == 2:
d_a = -d_a
m_a = -m_a
s_a = -s_a
dir_x = neg_string
# north or east - positive
elif ns_float==1:
dir_x = pos_string
else:
raise InvalidInputError("1 or 2!")
# return d_a, m_a, s_a, dir_x
return d_a + m_a / 60.0 + s_a / 3600.0
def enter_degrees_loop(input_what, pos_string, neg_string):
while True:
try:
return enter_degrees(input_what, pos_string, neg_string)
except InvalidInputError, e:
print e # output the error
continue
def enter_latitude():
return enter_degrees_loop("Latitude", "North", "South")
def enter_longitude():
return enter_degrees_loop("Longitude", "East", "West")
print "***Degrees/Minutes/Seconds >>> Decimal Degrees***"
print
latitude = enter_latitude()
longitude = enter_longitude()
print
print "Results:"
print
print "Latitude: ", latitude
print "Longitude: ", longitude
print
如您所见,我做了一些额外的更改:
- 我将转换从
float()
更改为int()
:在这里使用非整数没有任何意义
- 我已将
打印语句放在它们所属的位置
- 我使用一个异常来指示输入错误,并将另一个层环绕该all循环,直到正确输入all
- 我将
dua*(-1)
替换为-dua
,dua*1
替换为dua
- 我将计算移到最里面的函数中
仍然可以做的事情:
- 检查度数、分和秒的范围是否正确
- 省略不需要的内容(例如
dir\ux
)
这是一个副本。请[here][1]查找解决方案。[1] :@cleros:我不认为这是一个重复的问题……或者至少与另一个问题有足够的不同,可以保证不被关闭。顺便说一句,要在评论中添加内联链接,请使用类似于[Google]的内容(http://www.google.com/)
。这是一个副本。请[here][1]查找解决方案。[1] :@cleros:我不认为这是一个重复的问题……或者至少与另一个问题有足够的不同,可以保证不被关闭。顺便说一句,要在评论中添加内联链接,请使用类似于[Google]的内容(http://www.google.com/)
。在我(和许多其他人)的选项中,这种方法比让函数在其他函数中引用变量或让它们都访问(可能会更改)全局变量要好