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.
Trueъ object-oriented way would be to create one base class (probably abstract, but not necessarily) and two concrete implementations - aka [Strategy pattern](http://en.wikipedia.org/wiki/Strategy_pattern). Than instead of directly instantiating those classes with `my_object = MyClass()` you create "factory" method to instantiate the correct implementation and return it to the callee - aka [Factory pattern](http://en.wikipedia.org/wiki/Factory_method_pattern)

Something like that:

```
class BaseHandler(object):
    pass

class OSXHandler(BaseHandler):
    pass

class WindowsHandler(BaseHandler):
    pass

def create_handler():
    import sys
    return OSXHandler() if sys.platform == 'darwin' else WindowsHandler()

handler = create_handler()
handler.get_home_directory()

```