problems with class initiation

Magnus Lie Hetland mlh at idi.ntnu.no
Sun Jul 1 22:15:48 CEST 2001


"Iñigo Serna" <inigoserna at terra.es> wrote in message
news:mailman.994017363.31520.python-list at python.org...
[snip]
> 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.

The assignment isn't finished, so app is indeed None. Instead of
relying on global variables, you should supply app as a parameter
to the constructor of Second, IMO... Thus:

class Second:
    def __init__(self, app):
        return self.init_fn(app)
    def init_fn(self, app):
        return app.prefs.a

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

... or something like that. (The design seemed slightly confusing to me,
but I think this should work, at least :)

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list