调用copy时仍链接python类变量

调用copy时仍链接python类变量,python,python-3.x,oop,python-object,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):

我有以下课程:

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\u rent.flows
字典在第一次调用时正确实例化,并且不会在我的
spv.building\u untach\u data
调用中更改,但当触发
spv.building\u tax\u cashflows
时,
spv.rev\u rent.flows
更改

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

现在,正如我所看到的,
spv.rev\u rent
spv.exp\u maintenance
应该通过
copy()
更加相关,但导致意外行为的是税务呼叫


快把我逼疯了-非常感谢您的帮助

为什么创建一堆类变量只是为了在
\uuuu init\uuuuu
中使用实例变量对它们进行阴影处理?无论如何,您的
现金流计划。流
是一个类变量,因此它当然是跨实例共享的。对于
SinglePropertyValuation
中的大多数变量也是如此,除了一些方法中隐藏的变量。Python不是Java。看一个关于Python的教程classes@juanpa.arrivillaga是的,可惜不是。你知道有什么好的教程吗,或者你能提供一个基本的例子作为答案吗?如果你真的想在类级别定义属性,但让它们像实例属性一样工作,那么你可能真的想要一个
@dataclass
@attr.s
类。但是如果你没有一个很好的理由,只是因为你想把Python变成Java,那么你应该学习用每种语言编写习惯用语。Python是一个非常糟糕的Java,Java是一个非常糟糕的Python,但是Python是一个伟大的Python,Java是一个非常好的Java。