'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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|