CustomClass复制错误:AttributeError:“CustomClass”对象没有属性“copy”

2024-09-29 23:22:53 发布

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

我在它自己的.py文件中有以下类:

import pandas as pd

class CashFlowSchedule(object):
    flows = {}
    annual_growth_rate = None
    df = None

    def __init__(self, daterange, months=range(1, 13), amount=0, growth=0, growth_month=1):

        self.annual_growth_rate = growth

        for dt in daterange:

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


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

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

当我打电话给:

^{pr2}$

我得到:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-c96b6d8d0ab0> in <module>()
----> 1 x.copy()

AttributeError: 'CashFlowSchedule' object has no attribute 'copy'

出了什么问题,我在这里遗漏了什么?在

这个类非常原始,我认为__copy__应该存在于object方法中。在

谢谢你


Tags: inselfnonedfobjectratedtamount
1条回答
网友
1楼 · 发布于 2024-09-29 23:22:53

{{cd2>没有问题的方法。在

假设您想要创建CashFlowSchedule的副本,那么应该使用Python的copylibrary。在

要创建浅拷贝:

import copy
import cf_schedule as cfs
x=cfs.CashFlowSchedule(pd.date_range('20180101','20190101'))
x_copy = copy.copy(x)

要创建深度副本,只需将最后一行替换为:

^{pr2}$

相关问题 更多 >

    热门问题