On Wed, Aug 26, 2009 at 12:25 PM, Mac Ryan<quasipe...@gmail.com> wrote: > Hello everybody, > > I am using "storm" (https://storm.canonical.com/) to manage my > database. In storm, relationships between tables (each table is > represented by a class) are expressed like this (line #4): > > 1 >>> class Employee(Person): > 2 ... __storm_table__ = "employee" > 3 ... company_id = Int() > 4 ... company = Reference(company_id, Company.id) > > where Company is another class. Now, what I noticed is that Company must > be declared as a class before Employee, or python will throw an > exception (Company is not defined). > > I would be interested in understanding why this is so designed. I > expected that the exception would not be thrown at all, as I imagined > that the interpreter simply kept track of where classes were declared > and would try to evaluate the code only once an actual object would be > instantiated (at that point the interpreter would know where to look for > each class code).
The body of a class definition is executed when it is encountered. The result is a class object (an instance of 'type', usually). Any names defined at class scope become attributes of the class. The class name becomes a reference to the class object. > BTW, the behaviour I am describing is exactly what happens with function > declaration: the following code evaluates as expected, indeed. > > def fone(): > ftwo() > def ftwo(): > print "hello" > fone() Yes, functions are different than classes. The body of a function is not executed until it is called. Note that class methods behave like functions (well, they are functions) - they are not executed until called. But statements at class scope are executed to create the class. > > I would also be interested in knowing if there is a way around this or > if I simply have to live with it. You have to live with it unless you can put the attributes inside a method. In this case, I don't think that will work. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor