Stack Exchange
using kwargs in classes with python
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

I think it's better to pass them explicitly in this case.

you could use inheritance here: Create a base class that has all the shared attributes, and make the 2 existing classes inherit from that base class. Or create a class that only has the shared attributes and instantiate that class as an attribute of the 2 existing classes. Then you would only have to pass this "shared attribute class" between the 2 classes.
