The order of classes

Daniel Klein danielk at aracnet.com
Mon Mar 19 09:00:40 EST 2001


Is it a requirement to place classes in a particular order if one class uses
another in its __init__ method?

To illustrate this in its simplest form...

class bar:
    def __init__(self, f = foo()):
        pass

class foo:
    def __init__(self):
        pass

...then at the interactive prompt...

>>> import foobar
Traceback (innermost last):
  File "<pyshell#0>", line 1, in ?
    import foobar
  File "C:\DanielK\python\foobar.py", line 1, in ?
    class bar:
  File "C:\DanielK\python\foobar.py", line 2, in bar
    def __init__(self, x = foo()):
NameError: There is no variable named 'foo'

...however, it works if I reverse the order of the classes and put 'foo' before
'bar'.

I would like to keep my classes in a particular order but it does not appear
that I can do this in all cases.

Thanks,
Daniel Klein



More information about the Python-list mailing list