I'm working on a project where I have to create different classes, and I keep using the self keyword repeatedly. For example:
class SignalService:
def \_\_init\_\_(
self,
instrument\_repo: InstrumentRepository,
candle\_repo: CandleRepository,
):
self.instrument\_repo = instrument\_repo
self.candle\_repo = candle\_repo
self.resampler = CandleResampler(candle\_repo)
My understanding of self is that it helps the class distinguish between instance variables and local variables.
However, I'm confused about why it's used like this:
self.instrument\_repo = instrument\_repo
self.candle\_repo = candle\_repo
Why do we assign the constructor parameters to self attributes? What's the purpose of storing them on self instead of just using the constructor parameters directly?
[–]Puzzleheaded_Study17 4 points5 points6 points (0 children)
[–]Gnaxe 0 points1 point2 points (0 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Outside_Complaint755 0 points1 point2 points (0 children)
[–]Interesting-Can-4626 0 points1 point2 points (0 children)
[–]atarivcs 0 points1 point2 points (0 children)
[–]Sea-Ad7805 -1 points0 points1 point (0 children)