problems with class initiation

Nick Perkins nperkins7 at home.com
Mon Jul 2 03:20:16 CEST 2001


You could pass a reference to your 'prefs' to the constructor of 'Second'..

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

Now class Second keeps it's own reference to the 'prefs' object, instead of
relying on using some other binding to the same object.

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

        # depending what you wanted, either of:
        self.fns.append(second.init_fn)   # appends the function itself,
or...
        self.fns.append(second.init_fn()) # calls the fn, and appends the
result

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

Since you have a list called fns, I wonder whether you intend to have a list
of functions, in which case, the first of the two commented lines does what
you want.

On a more general note, it looks like you are using classes for everything,
where some of these things might be better off as simple functions.  Unlike
java, not everything has to be a class.  (for starters, class 'main' should
probably be just a function:  def main(): ...






More information about the Python-list mailing list