在另一个函数中使用返回值 [英] Using return value inside another function

查看:39
本文介绍了在另一个函数中使用返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个功能.请看下面的功能;

I have got two functions. Please see the functions below;

 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)的返回值.

Just want to use returning value of (user_channel_number) in the following function.

   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 不是全局定义的?如何在 delete_events() 函数内调用 user_channel_number?

When I run my script, it says user_channel_number is not defined globally ? How can I call user_channel_number inside the delete_events() function?

推荐答案

函数不能共享它们的局部变量.您可以从第一个返回值并将其传递给第二个:

Functions can not share their local variables. You can return the value from the first and pass it to the second:

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

这篇关于在另一个函数中使用返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆