返回功能,真的很困惑

时间:2017-02-08 18:29:39

标签: python python-2.7 python-3.x

我是Python的新手,我几乎要学习其他人的代码并将其整合到我的代码中,所以我为丑陋道歉。

我有这个变量random_collector = ''。随着代码的继续,我将random_collector设置为非空字符串。最后,我为random_collector设置了返回功能。问题是:会返回空白字符串还是非空白字符串

def add_csv():
    random_collector = ''
    sitevalue = ''
    path = raw_input('path to csv file: ')
    with open(path, 'r') as ifile:
        reader = csv.reader(ifile)
        reader.next()
        for row in reader:
            deviceip = row[1]
            displayname = row[0]
            snmpc = row[2]
            alert_priority = row[3]
            monitoring_templates = row[4]
            pd_alert_service = row[5]
            site_code = row[6]
            team_owner = row[7]
            if displayname_pattern.match(displayname):
                cut_displayname = displayname[0:4]
                print cut_displayname
                if cut_displayname in site:
                    sitevalue = site.get(cut_displayname)
                    cut_office = cut_displayname[0:3]
                    if cut_office in na_office:
                        random_collector = random.choice(na_collector)
                        lm_add_api(deviceip, displayname, random_collector, sitevalue, snmpc, alert_priority, monitoring_templates, pd_alert_service, site_code, team_owner)
                        time.sleep(5)
                    elif cut_office in apac_office:
                        random_collector = random.choice(apac_office)
                        lm_add_api(deviceip, displayname, random_collector, sitevalue, snmpc, alert_priority, monitoring_templates, pd_alert_service, site_code, team_owner)
                    elif cut_office in emea_office:
                        random_collector = random.choice(emea_office)
                        lm_add_api(deviceip, displayname, random_collector, sitevalue, snmpc)
                else:
                    print 'Site format is not a recongnized'
                    time.sleep(2)
                    add()
            else:
                print 'Double check the site code, device group not found in Logic Monitor'
                time.sleep(2)
                add()

    return deviceip, displayname, random_collector, sitevalue, snmpc, alert_priority, monitoring_templates, pd_alert_service, site_code, team_owner

我在这段代码中混淆的第二部分是我正在调用另一个名为lm_add_api(deviceip, displayname, random_collector, sitevalue, snmpc, alert_priority, monitoring_templates, pd_alert_service, site_code, team_owner)的函数,它正在使用变量random_collector。它是从“返回”中获取变量还是从random_collector = random.choice(na_collector)获取结果?

if cut_office in na_office:
                        random_collector = random.choice(na_collector)
                        lm_add_api(deviceip, displayname, random_collector, sitevalue, snmpc, alert_priority, monitoring_templates, pd_alert_service, site_code, team_owner)

1 个答案:

答案 0 :(得分:1)

返回的random_collector的值将是返回时该变量中存储的任何值。如果代码到达并执行为random_collector分配值的代码行之一,那将是返回的值。如果没有,它将保留你给它的初始值(空字符串)。

对于第二个问题,您使用从random.choice(na_collector)返回的值调用lm_add_api。

值得注意的是,Python与其他命令式编程语言一样,在代码执行时更改状态并维护更新的状态值。因此,只要您更改变量的值,它就会保留更新的值。代码如:

x = 1
x = 2
x = 3
x = 4
print x

只会打印'4'。所有早期的作业都会被覆盖。