Write Python classes that have different behavior for Mac and Windows
Question: I want to be able to instantiate an object whose methods will behave
differently depending on the platform.

    
    
    import sys
    
    class MyClass(object):
    
        @property
        def os_is_darwin(self):
            return sys.platform == 'darwin'
    
        def get_home_directory(self):
            if self.os_is_darwin:
                return '/Users/travis/'
            else:
                return 'C:\\Users\\travis\\'
    

Is there a cleaner way to do this by using an abstract base class and dividing
the Mac and Windows implementations into subclasses? The important thing is
abstracting away the platform for the caller as the above class does:

    
    
    my_object = MyClass()
    print my_object.get_home_directory()
    


Answer: Is it necessary that `MyClass` really is a class, or could it just seem like a
class in that you can call it to create an object? Let's call it `MyObject` so
it doesn't sound entirely perverse, and you end up with something like this:

    
    
    def MyObject():
      import sys
      if sys.platform == 'darwin':
        return MyDarwinObject()
      else:
        return MyDefaultObject()
    
    my_object = MyObject()
    print my_object.get_home_directory()
    

This "quacks like a duck" in the way you are most likely to use, and allows
you to keep the actual different classes entirely separate if you wish. (If
you want to share functionality you can of course use inheritance.)

Naturally the if-else chain can be replaced with a more extensible approach
such as a dictionary.

