problems with class initiation

Iñigo Serna inigoserna at terra.es
Sun Jul 1 15:53:21 EDT 2001


Hello all,

I'm having problems with class initiation.
Look at this code:

*********************************************************************

#!/usr/bin/env python

app = None

class Preferences:
    def __init__(self):
        self.a = 1
 
class Second:
    def __init__(self):
        return self.init_fn()
    def init_fn(self):
        return app.prefs.a

class Main:
    def __init__(self):
        self.prefs = Preferences()
        self.fns = []
        self.fns.append(Second())

if __name__ == '__main__':
    app = Main()

*********************************************************************

And this is the error python gracefully returns:

Traceback (most recent call last):
  File "./test.py", line 29, in ?
    app = Main()
  File "./test.py", line 25, in __init__
    self.fns.append(Second())
  File "./test.py", line 13, in __init__
    return self.init_fn()
  File "./test.py", line 18, in init_fn
    return app.prefs.a
AttributeError: 'None' object has no attribute 'prefs'

*********************************************************************

I suppose the error comes due to Main class hasn't finished its
initiation when 'app.prefs.a' is executed so app is None in that moment.

But I need the value of 'prefs.a' in 'init_fn'... is there any solution?
(no, rewrite the whole code isn't a solution ;-)


Thanks in advance,

Iñigo Serna






More information about the Python-list mailing list