In [ ]:
class State_d:
    def __init__(self, imp):
        self.__implementation = imp
    def changeImp(self, newImp):
        self.__implementation = newImp
    # Delegate calls to the implementation:
    def __getattr__(self, name):
        return getattr(self.__implementation, name)

class Implementation1:
    def f(self):
        print("Fiddle de dum, Fiddle de dee,")
    def g(self):
        print("Eric the half a bee.")
    def h(self):
        print("Ho ho ho, tee hee hee,")

class Implementation2:
    foo =1 
    def f(self):
        print("We're Knights of the Round Table.")
    def g(self):
        print("We dance whene'er we're able.")
    def h(self):
        print("We do routines and chorus scenes")

In [ ]:
def run(b):
    b.f()
    b.g()
    b.h()
    b.g()

b = State_d(Implementation1())
run(b)

In [ ]:
b.changeImp(Implementation2())
run(b)

In [ ]:
b.foo = 100

In [ ]:
b.foo

In [ ]:
b = State_d(Implementation1())
run(b)

Expected -- no attribute foo in Implementation1.


In [ ]:
try:
    b.foo
except:
    print("no implementation")

In [ ]: