加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:11:38 所属栏目:Python 来源:网络整理
导读:我有两个功能.请参阅以下功能; 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 = ("c
我有两个功能.请参阅以下功能;

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:Usersyke01DocumentsStormTestScriptsCompleted'
                        'UtilsUPNP_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?

解决方法

函数无法共享其局部变量.您可以从第一个返回值并将其传递给第二个:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读