0

Here is the problem, I have a class that I would like to define in python to be able to pass the buck onto another class so to speak when a function to the primary class is called. For example:

class Multi_Interface:
    def __init__(self):
        interfaces = [Interface("message A"), Interface("Message B")]
        self.set_interface(0)

    def set_interface(self, interface_index = 0):
        current_interface = self.interfaces[interface_index]
    # @todo: override the .operator to call the member of the current interface.

class Interface:
    def __init__(self, message):
        self.msg = message

    def do_stuff(self, message="I am a parameter"):
        print message + self.msg

mi = Multi_Interface()
mi.do_stuff(message="the internal message is: ")
mi.set_interface(1)
mi.do_stuff(message="the internal message is: ")
mi.do_stuff()

>>> the internal message is: mesage A
>>> the internal message is: mesage B
>>> I am a parameter: mesage B

Is there a way to overload this operator for a class? How would one overload this operator? I have an idea on how to get the method from the child object provided I can get a string in a method.

Rusty Weber
  • 1,541
  • 1
  • 19
  • 32
  • methinks you want to subclass. Try starting off your Multi_Interface class like this: `class Multi_Interface(Interface)` This way Multi_Interface will inherit all of the methods defined in Interface, as long as you construct it the same way as you would an Interface... also you have other errors in your code, like `self.interfaces` does not exist. – TehTris Oct 30 '14 at 20:54
  • The problem with that solution is that the interfaces that I am dealing with are not always such concrete classes derived from an Interface class and will have lots of methods. Hence the reason I wish to take the call and attempt to pass it to the interface without having to explicitly override every method from a concrete interface class. – Rusty Weber Oct 30 '14 at 20:59
  • 1
    Personally i cant see a way to do this without subclassing ( Maybe make `Interface` a super bare minimum class )... if your still stuck on overloading `.` then according to http://stackoverflow.com/questions/1936135/operator-overloading-in-python it might be possible.. – TehTris Oct 30 '14 at 21:10
  • 1
    most of the time, there is a better, less magic solution to such problems. Can you ask more specific. – Daniel Oct 30 '14 at 21:12
  • I am looking for the special method that gets called when looking for members in a class after using a "." as in "instance.method_name(parameters)"; I am looking for that method so that I can override it of course. I suspect that it might be the attrgetter method on the following page, but I am not sure if that is the method that I need to override. https://docs.python.org/2/library/operator.html – Rusty Weber Oct 30 '14 at 21:20
  • the methodcaller function also looks very promosing from that document. – Rusty Weber Oct 30 '14 at 21:23
  • looking into the __getattribute__ method to see if that is the magic medecine.. – Rusty Weber Oct 30 '14 at 21:30

1 Answers1

0

final solution is as follows

class Multi_Interface:
    def __init__(self):
        self.interfaces = [Interface("message A"), Interface("Message B")]
        self.current_interface = None
        self.set_interface(0)

    def set_interface(self, interface_index = 0):
        self.current_interface = self.interfaces[interface_index]

    def __getattr__(self, name):
        return getattr(self.current_interface, name)

class Interface:
    def __init__(self, message):
        self.msg = message

    def do_stuff(self, message="I am a parameter "):
        print message + self.msg

mi = Multi_Interface()
mi.do_stuff(message="the internal message is: ")
mi.set_interface(1)
mi.do_stuff(message="the internal message is: ")
mi.do_stuff()
Rusty Weber
  • 1,541
  • 1
  • 19
  • 32