Created
April 9, 2013 01:50
-
-
Save majorz/5342271 to your computer and use it in GitHub Desktop.
The idea
This file contains 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
from __future__ import teleport | |
from abc import ABCMeta | |
class Renderer(metaclass=ABCMeta): | |
def __init__(self, entity): | |
self._entity = entity | |
@teleport | |
def __call__(self): | |
if isinstance(self._entity, self.__type): | |
return self.__render() | |
else: | |
return super().__call__() | |
class IntRenderer(Renderer): | |
__type = int | |
def __render(self): | |
return 'INTEGER {}'.format(self._entity) | |
class FloatRenderer(Renderer): | |
__type = float | |
def __render(self): | |
return 'FLOAT {}'.format(self._entity) | |
class CompositeRenderer( | |
IntRenderer, | |
FloatRenderer, | |
): | |
pass | |
if __name__ == '__main__': | |
numbers = [10, 4.1, 91.1, 1000] | |
for number in numbers: | |
renderer = CompositeRenderer(number) | |
print(renderer()) | |
''' | |
OUTPUT: | |
INTEGER 10 | |
FLOAT 4.1 | |
FLOAT 91.1 | |
INTEGER 1000 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment