在另一个函数中使用返回值

pylearner

我有两个功能。请参见下面的功能;

 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_numberdelete_events()函数内部调用?

尤金·普里马科(Eugene Primako)

函数不能共享其局部变量。您可以从第一个返回值,然后将其传递给第二个:

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:
         ....

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google 表格在另一个函数中使用匹配函数的返回值

使用另一个函数返回值

在另一个函数中使用的返回值-Python

在Golang中使用另一个函数返回值?

Javascript在另一个函数中使用返回值

如何在Javascript的另一个函数中使用返回值?

在c++中使用一个函数的返回值作为另一个函数的参数

如何在另一个函数中使用带有参数的函数的返回值

如何在对象文字中使用另一个JS函数的返回值

我想在另一个函数中使用一个函数的返回值,而无需再次运行该函数。(蟒蛇)

如何使用函数返回值作为PHP中另一个函数的参数?

使用constexpr函数的返回值作为另一个函数的参数

试图在另一个函数中使用一个函数的返回值,但得到未定义的变量

将WP移至另一个域->无法使用函数返回值

使用 Python 将返回值从一个函数传递到另一个函数

如何从异步函数返回多个值以在另一个函数中使用

如何在另一个函数中引用 2 个函数的返回值?

如何使用另一个函数的返回值作为另一个函数的参数?发现名称错误

当我在 Python 的另一个函数中使用它时,我可以隐藏它的某些返回值吗?

调用另一个模拟函数时更改模拟函数的返回值

将函数返回值分配为另一个函数参数

Angular 2 在启动另一个函数之前等待函数返回值

从另一个函数内的回调函数返回值

在函数中返回struct的值,并在另一个包中使用它

如何在另一个函数中使用从wxPython按钮返回的值?

在另一个函数中使用输入的值

如何将一个函数的输入值与另一个函数的返回值进行比较?

返回值到另一个活动

尝试从另一个表返回值