Export variables

msolem at linuxmail.org msolem at linuxmail.org
Tue Apr 21 17:56:32 CEST 2009


Hello,

I want to make a certain subset of public attributes available through
an interface.

The client should not need to know the names of the attributes.

Here is some code with the important parts missing.  Anyone care to
fill in the missing parts?


class C:
    def __init__(self):
        self.a = 4
        self.b = 6
        self.c = 8
        self._set_vars([self.a, self.b])  # This call might need to be
done differently

    def get_vars(self):
        # add code here...

    def _set_vars(self, vars):
        # add code here...


c = C()
print c.get_vars()
c.a = 9
print c.get_vars()


This is the desired output
[4, 6]   # Values of c.a and c.b
[9, 6]   # Values of c.a and c.b

The important part is that the change to c.a is reflected in the call
to get_vars().  get_vars() and _set_vars() might be moved to a base
class,
and therefore should not have attribute names hard coded in them.

I don't have a specific problem that I'm trying to solve.  I would
just like
to know how this sort of thing could be done.

Thanks,
Mike



More information about the Python-list mailing list