[Tutor] Declaration order of classes... why it is important?

Mac Ryan quasipedia at gmail.com
Wed Aug 26 18:25:52 CEST 2009


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).

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()

I would also be interested in knowing if there is a way around this or
if I simply have to live with it. 

It is not that this impede to achieve anything, but in reading code, I
normally prefer to have the big picture first [This is a house, as you
see is composed of walls, roof, basement. A wall can have...] while this
behaviour obliges me to write the code the other way around ["This is a
brick, if you put together bricks you get a wall, if you put together
walls you get..."]

Thanks in advance,
Mac.



More information about the Tutor mailing list