Python: looking for a short-hand way to setup setters/getters for lots of variables

Viewed 88

I have one class (Bar) embedded inside another class (Foo).

class Foo():
    class Bar():
        def __init__(self):
            self.a = 1
            self.b = 2
            ...
            self.z = 26


    def __init__(self):        
        self.bar = Bar()

To access the attributes of class Bar, the user would need to the following:

>>> f = Foo()
>>> f.bar.a
1

How can I setup a short dot notation so that users can use BOTH:

>>> f.bar.a
1

and

>>> f.a
1

In my example, I'm trying to demonstrate that Bar class has a lot of variables. So I don't want to write a getter/setter for each one manually. So I was thinking to use the property() in a for loop like this:

def __init__(self):
    self.bar = Bar()

    # Allow shorter dot notation
    for parm in self.bar.__dict__:
         setattr(self, i, getattr(bar, i))
         self.i = property(...)

But I'm unsure how to use property in this context without manually writing several setter functions.

Any suggestions on how to allow access to both shorter and longer notations?

1 Answers
Related