Python:如何通过传递参数从另一个文件导入变量

时间:2018-02-12 10:52:37

标签: python python-2.7

尝试独立于主文件构建代码列表

#### expense_codelist.py ####
def thelist(filename_list):
    fn_type = filename_list.split('-')
    pay_method_var = fn_type[1]
    if fn_type[0] == 'Chase':
        pay_method_val = 'Credit Card'
        card_name == 'Chase'
        proceed = 'Y'
    if fn_type[0] == 'Expenses':
        pay_method_val = 'Debit Card'
        card_name = 'Bancontact'
        proceed = 'Y'
        return
    if fn_type[0] == 'ING Credit Card':
        pay_method_val = 'Credit Card'
        card_name = 'ING'
        proceed = 'Y'
    if fn_type[0] == 'Capital One':
        pay_method_val = 'Credit Card'
        card_name = 'Capital One'
        proceed = 'Y'

我想使用变量pay_method_val,card_name,继续返回主脚本。我试过的是:

#### process.py ####
import expense_codelist
print expense_codelist.thelist(incsvfn)proceed

我收到以下错误:

print expense_codelist.thelist(incsvfn).proceed
AttributeError: 'NoneType' object has no attribute 'proceed'

2 个答案:

答案 0 :(得分:1)

您可以使用collections模块并输入

TheList = collections.namedtuple('TheList', 'pay_method_val, card_name, proceed')
return TheList(pay_method_val, card_name, proceed)

你的功能的结束,但这感觉有点像黑客。

你可能想要的是一个存储数据的类,它将被写成:

class TheList(object):
    def __init__(self, filename_list):
        fn_type = filename_list.split('-')
        self.pay_method_var = fn_type[1]
        if fn_type[0] == 'Chase':
            self.pay_method_val = 'Credit Card'
            self.card_name == 'Chase'
            self.proceed = 'Y'
        elif fn_type[0] == 'Expenses':
            self.pay_method_val = 'Debit Card'
            self.card_name = 'Bancontact'
            self.proceed = 'Y'
        elif fn_type[0] == 'ING Credit Card':
            self.pay_method_val = 'Credit Card'
            self.card_name = 'ING'
            self.proceed = 'Y'
        elif fn_type[0] == 'Capital One':
            self.pay_method_val = 'Credit Card'
            self.card_name = 'Capital One'
            self.proceed = 'Y'

会像这样调用:

import expense_codelist
thelist = expense_codelist.TheList(incsvfn)
print thelist.proceed

答案 1 :(得分:0)

如果你只想要那些变量的值(而不是包含它们的对象),你可以简单地return

#### expense_codelist.py ####
def thelist(filename_list):
    fn_type = filename_list.split('-')
    pay_method_var = fn_type[1]
    if fn_type[0] == 'Chase':
        pay_method_val = 'Credit Card'
        card_name == 'Chase'
        proceed = 'Y'
    elif fn_type[0] == 'Expenses':
        pay_method_val = 'Debit Card'
        card_name = 'Bancontact'
        proceed = 'Y'
        return
    elif fn_type[0] == 'ING Credit Card':
        pay_method_val = 'Credit Card'
        card_name = 'ING'
        proceed = 'Y'
    elif fn_type[0] == 'Capital One':
        pay_method_val = 'Credit Card'
        card_name = 'Capital One'
        proceed = 'Y'
    else:
        raise ValueError("Card type not recognized")
    return pay_method_var, pay_method_val, card_name, proceed

在您的其他文件中,只需将它们保存到本地变量:

#### process.py ####
import expense_codelist
pay_method_var, pay_method_val, card_name, proceed = expense_codelist.thelist(incsvfn)
print proceed

我还将您的if链设为if..elif..else链。你的字符串是互斥的(并且应该有代码处理卡类型是意外的情况)。