Created
October 7, 2020 17:23
-
-
Save abadger/439efd341ed5d2e29a04cda92dc7cbbe to your computer and use it in GitHub Desktop.
Writing a class which returns different instances depending on an external factor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 -tt | |
import abc | |
import os | |
class Base(metaclass=abc.ABCMeta): | |
@abc.abstractmethod | |
def run(self): | |
pass | |
class Other1(Base): | |
def run(self): | |
print('Other1') | |
class Other2(Base): | |
def run(self): | |
print('Other2') | |
class Test(Base): | |
def __new__(cls, *args, **kwargs): | |
if os.environ.get('TESTING', False): | |
return Other1(*args, **kwargs) | |
return Other2(*args, **kwargs) | |
Test().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment