在python的课堂中使用夸尔格斯

发布于 2025-01-26 18:48:45 字数 893 浏览 2 评论 0原文

我对在下面的类中使用kwargs的混淆

是我的代码

class Get_Index_Change_Data:
    def __init__(self, **kwargs):
        self.idx_chg(**kwargs)

    def idx_chg(self):
        url = 'https://xxx' + self.index_code + '/marketclosexxx=' + self.calc_date + '&xx=' + self.as_of_date
        self.response = API().connection(url=url)

class Get_Index_Changes:
    def __init__(self, index_code, calc_date, as_of_date, **kwargs):
        self.index_code = str(index_code)
        self.calc_date = calc_date
        self.as_of_date = as_of_date
        self.idx_data = Get_Index_Change_Data(**kwargs)

,我想要self.index_code,self.calc_date,self.as_of_date,当我调用它时,我会流入get_index_change_data类中。

我知道我可以在get_index_change_data类本身中分配属性,但是当我在两个单独的类中调用相同属性时,它看起来很混乱。我以为夸尔格可以用作过滤过程,但是我

在运行上述代码时如何处理它,我会遇到错误-AttributeError:'get_index_change_data'对象没有属性'index_code'

谢谢

I am getting really confused with using kwargs in classes

below is my code

class Get_Index_Change_Data:
    def __init__(self, **kwargs):
        self.idx_chg(**kwargs)

    def idx_chg(self):
        url = 'https://xxx' + self.index_code + '/marketclosexxx=' + self.calc_date + '&xx=' + self.as_of_date
        self.response = API().connection(url=url)

class Get_Index_Changes:
    def __init__(self, index_code, calc_date, as_of_date, **kwargs):
        self.index_code = str(index_code)
        self.calc_date = calc_date
        self.as_of_date = as_of_date
        self.idx_data = Get_Index_Change_Data(**kwargs)

I want the attributes self.index_code, self.calc_date, self.as_of_date to flow into Get_Index_Change_Data class when I call it.

I know I can assign attributes in the Get_Index_Change_Data class itself but it looks messy as im calling the same attributes in 2 separate classes. I thought kwargs could be used as a filtering process but I'm stuck on how to do it

when running the above code I get an error - AttributeError: 'Get_Index_Change_Data' object has no attribute 'index_code'

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文