gpt4 book ai didi

python - 如何在Python中调用另一个方法中的方法变量

转载 作者:行者123 更新时间:2023-12-01 07:59:38 31 4
gpt4 key购买 nike

我在一个类中有两个方法。我想在另一个方法中调用一个方法的变量。这是我的代码:

#第一种方法:

def starter_loan_check(self):
#global total_offer,final_offer
#final_offer=[]
for i in self.overall_data:
if i[1]=='PS' and i[3] in conf.PS_State and i[5]>29:
if i[2]!='challengerTUFT':
for c in cutoff_config:
if i[3]==c['State'] and i[2]==c['challenger'] and i[4]<['CUTOFF3'] and i[4]>=['CUTOFF4']:
if i[7]>50:
global total_offer,final_offer
final_offer=[]
total_offer=[i for i in product(*[conf.i[3]['Loan_Amount'],conf.i[3]['Apr'],conf.i[3]['Term']])
#global total_offer,final_offer
#final_offer=[]
for t in total_offer:
if t[0]>min(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
final_offer.append(t)
elif t[0]>max(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
t[0]=max(i[3]['Loan_Amount'])
final_offer.append(t)
elif i[9]>75:
if t[0]>min(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
final_offer.append(t)
elif t[0]>max(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
t[0]=max(i[3]['Loan_Amount'])
final_offer.append(t)

我正在尝试在此处访问 Final_offer:第二种方法:

def starter_loan_logic(self):
for i in self.query1_data:
for j in final_offer:
if j[0]/(i[3]*.85)>.30:#LTI Check
final_offer.remove(j)

if i[2]==j[0] and i[4]=='BI_WEEKLY':
PTI=i[5]/((i[3]*0.85)/12)>0.20
final_offer.remove(j)

if i[2]==j[0] and i[4]=='TWICE_PER_MONTH':
PTI=i[6]/((i[3]*0.85)/12)>0.20
final_offer.remove(j)

if i[2]==j[0] and i[4]=='MONTHLY':
PTI=i[7]/((i[3]*0.85)/12)>0.20
final_offer.remove(j)

for k in self.overall_data:
k['state']=='IL' and i[5]/((i[3]*0.85)/12)>0.225
final_offer.remove(j)

但是我在 Total_offer 中的 for t 处收到无效语法错误在这里我尝试了 Global 但我不确定我是否正确使用它。请帮助我理解如何在另一个函数中访问一个函数的变量。

最佳答案

虽然可以使用全局变量来获得您想要的结果,但这不是推荐的方法。

您想要做的是将这些变量作为调用者设置的参数传递到函数中。所以第一个方法应该是这样的:

def starter_loan_check(self, total_offer):
final_offer = []
<< do loan check loop as above - but don't initialize final_offer in the loop>>
return final_offer

第二个循环看起来像:

def starter_loan_logic(self, final_offer):
<<do you loop here>>

无论您在何处调用这两个函数,都需要传递变量,例如:

total_offer = <<something>>
final_offer = self.starter_loan_check(total_offer)
self.starter_loan_logic(final_offer)

关于python - 如何在Python中调用另一个方法中的方法变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55782284/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com