我有两个功能。请参阅以下功能;
def check_channel_number(self):
print "***************Channel Checker *********************"
print ''
user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3digit): "))[:3]);
channel = ("channelNr= '%d'") % (user_channel_number)
print channel
# channel_search = channel + str(user_channel_number)
datafile = file('output.txt')
found = False
for line in datafile:
if channel in line:
found = True
print 'The channel number you entered is correct and will be deleted'
return user_channel_number
print 'The channel number you entered is not on the planner'
return False
只想在以下函数中使用(user_channel_number)的返回值。
def delete_events(self):
if user_channel_number == True:
return 'The program number is correct and will be deleted'
# action = 'DeleteEvent'
menu_action = 'all'
book = 'RECYC:687869882'
arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
'\\Utils\\UPNP_Client_Cmd_Line.py')]
arg_list.append(' --action=')
arg_list.append(action)
arg_list.append(' --ip=')
arg_list.append('10.10.8.89')
arg_list.append(' --objectId=')
arg_list.append(book)
subprocess.call(["python", arg_list])
print 'The program deleted successfully'
当我运行我的脚本时,它说全局没有定义user_channel_number
?如何在user_channel_number
函数中调用delete_events()
?
答案 0 :(得分:6)
在函数内部定义变量时,它是一个局部变量,这意味着它只能在该函数中访问。
在课堂内
看起来你在一个类中,所以你可以通过这样定义它来使变量可以被类中的所有方法访问:
def check_channel_number(self):
self.user_channel_number = ...
然后在第二个函数中,您可以像下面这样使用它:
def delete_events(self):
if self.user_channel_number:
课外
如果您没有在课程中使用方法,则可以使用global
内置语言。
例如,
def check_channel_number():
global user_channel_number
user_channel_number = ...
def delete_events():
if user_channel_number:
...
使用函数返回的值
相反,在第一个函数check_channel_number()
中,您可以让它返回user_channel_number
。然后,您可以在delete_events()
内调用该函数,如下所示:
def check_channel_number():
user_channel_number = ...
return user_channel_number
def delete_events():
if check_channel_number():
...
答案 1 :(得分:4)
函数无法共享其局部变量。您可以从第一个返回值并将其传递给第二个:
def check_channel_number(self):
...
return user_channel_number
def delete_events(self):
user_channel_number = self.check_channel_number()
...
或者在对象上保存价值:
def check_channel_number(self):
...
self.user_channel_number = user_channel_number
...
def delete_events(self):
if self.user_channel_number:
....
答案 2 :(得分:1)
所以我认为当你调用check_channel_number
函数时,user_channel_number
被定义在那里,所以当你调用delete_events
时,它已经超出了范围,也许这样会有所帮助?
user_channel_number = check_channel_number()
delete_events()
我可能也有user_channel_number作为删除函数的输入,所以它会变成这样:(其中ucn是user_channel_number
)
def delete_events(self, ucn):
if ucn == True:
print 'The program number is correct and will be deleted'
# action = 'DeleteEvent'
menu_action = 'all'
book = 'RECYC:687869882'
arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
'\\Utils\\UPNP_Client_Cmd_Line.py')]
arg_list.append(' --action=')
arg_list.append(action)
arg_list.append(' --ip=')
arg_list.append('10.10.8.89')
arg_list.append(' --objectId=')
arg_list.append(book)
subprocess.call(["python", arg_list])
print 'The program deleted successfully'
我也改变了`return'程序编号是正确的,将被删除''到print语句,因为我觉得返回将在其他代码行运行之前结束该函数
所以代码可能最终会像:
user_channel_number = check_channel_number()
delete_events(user_channel_number)
编辑:
注意到你的函数看起来像是一个类的一部分,
在这种情况下,您可以这样做:
self.ucn = self.check_channel_number()
self.delete_events(self.ucn)
(或者如果您不想将user_channel_number传递给该功能,您可以将if user_channel_number:
更改为if self. user_channel_number: