Let's say I have a python file test.py that looks like this:
class MyFirstClass():
prop=3
class MySecondClass():
prop1 = 0.
prop2 = MyFirstClass()
Then I can run this and it works:
from my_test import MyFirstClass, MySecondClass
obj = MySecondClass()
But if I write my python file like this, just inverting the definition order:
class MySecondClass():
prop1 = 0.
prop2 = MyFirstClass()
class MyFirstClass():
prop=3
then I get an error saying that MyFirstClass is not defined.
So Python obviously expect MyFirstClass to be defined before it is used in MySecondClass.
But I'm just wondering why it is like that. After all, function definitions with def can be made in any order within a module. So I'm (perhaps naively) a bit surprised that class definitions must be made in order like this.
I suspect that this has something to do with the fact that I used MyFirstClass to initialize a class-level attribute of MySecondClass. But could someone clarify what is going on here?