lundi 3 août 2020

Applying reflection to indirectly call objects from another class

I'm trying to make a class like Proxy that I indirectly access all methods of my objects, for example like:

class Radio():   
    def __init__(self):        
        self._channel = "channel"        

    def get_channel(self):        
        return self._channel    

    def set_channel(self, value):        
        self._channel = value   

class Proxy:
    def __init__(self, obj):        
        self.obj = obj

    # rest of the code


radio = Radio()
radio_proxy = Proxy(radio)
print(radio_proxy.get_channel())     

So this works exactly as print(radio.get_channel()), but I'm actually stuck how to do this, I know that it's somehow I should use getattr() and stuff, but I don't really know how to use them.





Aucun commentaire:

Enregistrer un commentaire