尽管调用了copy

时间:2018-08-24 00:36:34

标签: python python-3.x oop python-object

我有以下课程:

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时,{{1} } 更改

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

现在,在我查看的过程中,spv.rev_rent.flowsspv.rev_rent应该通过spv.exp_maintenance更加相关,但这是征税活动引起的意外行为。

让我发疯-任何帮助都真的赞赏!

0 个答案:

没有答案