#### 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'
I want to use the variables pay_method_val, card_name, proceed to be returned to the main script. What I tried is below:
#### process.py ####
import expense_codelist
print expense_codelist.thelist(incsvfn)proceed
I am getting the below Error:
print expense_codelist.thelist(incsvfn).proceed
AttributeError: 'NoneType' object has no attribute 'proceed'
You could use the collections module and put
TheList = collections.namedtuple('TheList', 'pay_method_val, card_name, proceed')
return TheList(pay_method_val, card_name, proceed)
a the end of your function, but this feels like a bit of a hack.
What you probably want is a class which stores the data, and would be written like this:
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'
which would be called like so:
import expense_codelist
thelist = expense_codelist.TheList(incsvfn)
print thelist.proceed
If you just want the values of those variables (and not an object containing them), you can simply return
them:
#### 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
And in your other file just save them to local variables:
#### process.py ####
import expense_codelist
pay_method_var, pay_method_val, card_name, proceed = expense_codelist.thelist(incsvfn)
print proceed
I also made your if
chain an if..elif..else
chain. Your strings are mutually exclusive (and there should be code handling the case where the card type is something unexpected).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.