在Python中通过多个类传递属性

2024-09-28 19:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将纳税申报表数据的属性传递给我创建的税务计算器。例如,我想通过10万英镑的工资。这里是我到目前为止的工资,但我似乎无法让它发挥作用:

class TaxReturn:

   def __init__(self, income = None, stat_adj = None, AGI = None):

             income = income or ['salaries_wages', 'interest_received', 'tax_exempt_interest_income', 'dividend_AGI', 'qualified_dividend']
             stat_adj = stat_adj or ['deductible_IRA_payments', 'student_loan_interest_deduc', 'education_expenses', 'tuition_fees_deduc', 
             'self_employ_deduc', 'self_employ_healthinsur_deduc', 'domestic_production_deduc']
             AGI = AGI or 'AGI'

             self.income = 'income'
             self.stat_adj = 'stat_adj'
             self.AGI = 'AGI'
class Income:
    def __init__(self= None, salaries_wages= None, intr_rec= None, txexem_intinc= None, dividend_AGI= None, qualified_dividend= None):    

            salaries_wages = salaries_wages or 'salaries_wages'
            intr_rec = intr_rec or 'intr_rec'
            txexem_intinc = txexem_intinc or 'txexem_intinc'
            dividend_AGI = dividend_AGI or 'dividend_AGI'
            qualified_dividend = qualified_dividend or 'qualified_dividend'

class TaxCal:

   def __init__(self):
      self.brackets = {(0,8025):0.10, (8025,32550):.15, (32550,78850):.25, (78850, 164550):.28, (164550,357700):.33, (357700,371815):.35, (371815, sys.maxsize):.396}     

   def taxcal (self, salaries_wages):
      tax = 0       
      for bracket in self.brackets:
        if salaries_wages > bracket[0]:
          for _ in range(bracket[0], min(salares_wages, bracket[1])):
              tax += self.brackets[bracket]
   return tax

tx =  TaxReturn()
inc = Income()
txcal = TaxCal()
print(tx.inc.txcal.taxcal(100000)), format(round(tx.inc.txcal.taxcal(100000), 2)

Tags: orselfnonedefstatdividendtaxagi
1条回答
网友
1楼 · 发布于 2024-09-28 19:00:53

注意你的缩进和课堂设计。我不知道你为什么在你的类中分配未知变量。除非你让它成为课堂的一部分,否则它毫无用处:

import sys

class Income:
    def __init__(self= None, salaries_wages= None, intr_rec= None, txexem_intinc= None, dividend_AGI= None, qualified_dividend= None):    

        self.salaries_wages = salaries_wages or 'salaries_wages'
        self.intr_rec = intr_rec or 'intr_rec'
        self.txexem_intinc = txexem_intinc or 'txexem_intinc'
        self.dividend_AGI = dividend_AGI or 'dividend_AGI'
        self.qualified_dividend = qualified_dividend or 'qualified_dividend'

class TaxCal:

   def __init__(self):
      self.brackets = {(0,8025):0.10, (8025,32550):.15, (32550,78850):.25, (78850, 164550):.28, (164550,357700):.33, (357700,371815):.35, (371815, sys.maxsize):.396}     

   def taxcal (self, inc):
       tax = 0       
       for bracket in self.brackets:
           if inc.salaries_wages and inc.salaries_wages > bracket[0]:
              for _ in range(bracket[0], min(inc.salaries_wages, bracket[1])):
                  tax += self.brackets[bracket]
       return tax

tx =  TaxReturn()
inc = Income(100000)
txcal = TaxCal()
print(txcal.taxcal(inc))

相关问题 更多 >