调用copy时链接的python类变量

2024-09-25 00:26:52 发布

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

我有以下课程:

import pandas as pd


class CashFlowSchedule:
    flows = {}
    annual_growth_rate = None
    df = None

    daterange = None
    months = None
    amount = None
    growth = None
    growth_month = None
    name = None

    def copy(self):
        return CashFlowSchedule(daterange=self.daterange, months=self.months, amount=self.amount, growth=self.growth,
                                growth_month=self.growth_month)

    def render_named_df(self):
        temp = self.df.copy()
        temp.columns = [self.name]
        return temp

    def re_init_df(self):
        self.df = pd.DataFrame([self.flows]).T

    def __init__(self, daterange=pd.date_range('19900101', '20010101'), months=range(1, 13), amount=0, growth=0,
                 growth_month=1, name=None, years=None):

        self.daterange = daterange
        self.months = months
        self.amount = amount
        self.growth = growth
        self.growth_month = growth_month
        self.name = name

        self.annual_growth_rate = growth

        if years is None:
            years = list({x.year: 0 for x in daterange}.keys())

        for dt in daterange:

            if dt.month == growth_month:
                amount *= (1. + self.annual_growth_rate)

            if dt.month in months and dt.year in years:
                self.flows[dt] = amount
            else:
                self.flows[dt] = 0.

        self.df = pd.DataFrame([self.flows]).T

以及另一个模块中的另一个类:

class SinglePropertyValuation:
    # descriptive
    desc_zipcode = None
    desc_property_type = None
    desc_units = None
    desc_beds = None
    desc_smooth_annual_expenses = None

    # dates
    date_purchase = None
    date_sale = None
    date_distance_months = None
    date_daterange_ownership = None

    # revenue
    rev_rent = None

    # recurring expenses
    exp_taxes = None
    exp_insurance = None
    exp_maintenance = None
    exp_management = None
    exp_hoa = None

    cash_flows = {}

    monthly_raw_rents = []

    desc_nearby_zips = None

    def establish_rent_cashflows(self, monthly_per_unit_rent, growth_rate=None,
                                 growth_month=None, default_growth_rate=.02):

        self.rev_rent =copy.copy(cfs.CashFlowSchedule(daterange=copy.copy(self.date_daterange_ownership),
                                             amount=monthly_per_unit_rent * self.desc_units, growth=growth_rate,
                                             growth_month=growth_month, name='rev_rent'))    

    def establish_tax_cashflows(self, annual_tax, growth):

        if self.desc_smooth_annual_expenses:
            self.exp_taxes = copy.copy(cfs.CashFlowSchedule(daterange=self.date_daterange_ownership, amount=annual_tax / 12.,
                                                  growth=growth, growth_month=5, name='exp_taxes'))
        else:
            self.exp_taxes = copy.copy(cfs.CashFlowSchedule(daterange=self.date_daterange_ownership, amount=annual_tax,
                                                  months=[4],
                                                  growth=growth, growth_month=5, name='exp_taxes'))

    def establish_vacancy_data(self, override_vacancy_rate=None):

        self.exp_vacancy_data = self.rev_rent.copy()
        self.exp_vacancy_data.flows = {dt: self.exp_vacancy_data.flows[dt] * override_vacancy_rate for dt in
                                       self.exp_vacancy_data.flows.keys()}

        self.exp_vacancy_data.re_init_df()
        self.exp_vacancy_data.name = 'exp_vacancy'

当我打电话时:

spv = SinglePropertyValuation()
spv.establish_rent_cashflows(monthly_per_unit_rent=1063, growth_rate=.01)
spv.establish_vacancy_data(override_vacancy_rate=1. / 24.)
spv.establish_tax_cashflows(annual_tax=8000., growth=.01)

我的spv.rev_rent.flows字典在第一次调用时正确实例化,在我的spv.establish_vacancy_data调用时,不会更改,但当spv.establish_tax_cashflows被触发时,spv.rev_rent.flows会更改

如何纠正这种行为?通过谷歌搜索和反复试验,我意识到这是一个常见的错误,但我不知道如何修复我的错误,因为大多数答案建议复制(我正在做)

现在,在我看来,spv.rev_rentspv.exp_maintenance应该通过copy()更加相关,但正是税务调用导致了意外行为

快把我逼疯了-谢谢你的帮助


Tags: nameselfnonerateamountcopygrowthmonth