On Wed, Aug 26, 2009 at 11:25 AM, 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). > > 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() Try it with a class definition instead: In [1]: class One: ...: Two() ...: ...: --------------------------------------------------------------------------- NameError Traceback (most recent call last) C:\Documents and Settings\Wayne\<ipython console> in <module>() C:\Documents and Settings\Wayne\<ipython console> in One() NameError: name 'Two' is not defined Yet with a function inside a class: In [2]: class One: ...: def spam(self): ...: Two() ...: ...: No problems. My guess is that it has to do with the difference between class and function definitions. AFAIK, classes are simply another namespace exactly like the global namespace. Similar to an 'import' statement which executes all the code found in that module. Further testing verifies this: In [3]: class One: ...: print "Hello" ...: ...: Hello Of course usually with a class you declare an __init__ method (function) that would eliminate your problem. The __init__ method is called on object instantiation. In [4]: class One: ...: def __init__(self): ...: Two() ...: ...: In [5]: One() --------------------------------------------------------------------------- NameError Traceback (most recent call last) C:\Documents and Settings\Wayne\<ipython console> in <module>() C:\Documents and Settings\Wayne\<ipython console> in __init__(self) NameError: global name 'Two' is not defined HTH, Wayne p.s. The In[#] instead of >>> are because I'm using Ipython. > > > 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. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor