[Tutor] Getting class method variable values

Alan Gauld alan.gauld@blueyonder.co.uk
Sat Jun 14 03:27:02 2003


> Each method contains a list of the property names for which
> values are calculated (the name of the list is the same for all
methods).
>
> What I want to do is to be able to return to the user a dictionary
> with all of the properties (for all methods) as keys and the values
> as the computed values (or None if they haven't been calculated).

Wouldn't it be easier to store the lists at the class level rather
than as local variables. Possibly in a dictionary keyed by method?
Then in each method you can fetch the list from the dictionary,
and outside the method you have access.

Something like:

class C:
     self.d = {'foo': {a:None,
                       b:None,
                       c:None},
               'bar': {a:None,
                       b:None}
               }

     def foo(self):
        vars = self.d['foo']
        vars[a] = 42
        # etc...

     def bar(self):
        vars = self.d['bar']
        # etc...

     def printVals(self):
        for meth in self.d:
           for var in meth:
              print meth[var]

Is that (remotely) like what you want to do?

Alan G