1

I want to create two classes (Spectrogram and TimeWindow) which should recall one another, but I'm not sure about my definitions. I first define Spectrogram and then TimeWindow afterwards. The logic when using the objects is the following:

  1. The window = TimeWindow() object is created
  2. I have a method which creates a spect = Spectrogram() object, and sets it as attribute of window
  3. I set the window object as attribute of the spect object, because I want to be able to get one from the other.

First question

The code seems to work. Is this architecture ok? Is there a cleaner way to achieve the fact that I can access one object from the other?

Second question

I tried to define the Spectrogram class as follows, imposing that the input variable time_window is of type TimeWindow. This results in an error, because I define the TimeWindow class after the Spectrogram class. Is there any way to impose the type of time_window?

class Spectrogram:

    def __init__(self, time_window:TimeWindow, ...):

        self.time_window = time_window
        ...


Thank you everyone!

2
  • I think in your case (two classes in the same file), you simply need to put the type in quotes. time_window: "TimeWindow". You can also do from __future__ import annotations at the top of your file Commented Sep 6, 2024 at 15:38
  • 1
    Thank you @Kaia, I added ` from future import annotations` ad it solved my problem. It was actually similar to the other question you pointed out. Commented Sep 9, 2024 at 8:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.