I have encountered a reference problem like this Example 1;
@dataclass
class Book:
book_id:int
book_name:str
book_library: Library #The object where book is stored
@dataclass
class Library:
library_id:int
library_capasity: int
book_list: list[Book]
In this example shown in above i encountered the Library object is not defined because it is defined after Book class declaration.
To overcome this problem i added a code block like this Example 2;
@dataclass
class Library:
pass
class Book:
book_id:int
book_name:str
book_library: Library #The object where book is stored
@dataclass
class Library:
library_id:int
library_capasity: int
book_list: list[Book]
After this there were no error.
My questions are listed as below;
- The method which i used to overcome the problem is forward declaration. Is it a bad code design?
- Python is an interpreted language and is being interpreted language causes this error which is occurred in Example 1?
- Can same error in Example 1 might happen in Java or C++ which are compiler based programming languages?