Created
October 12, 2019 07:03
-
-
Save adolli/77aeb242c0928bf511181a6dbe57b421 to your computer and use it in GitHub Desktop.
Generic types in python
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
class New(object): | |
def __init__(self): | |
super(New, self).__init__() | |
self._type = None | |
def __lt__(self, t): | |
self._type = t | |
return New2(t) | |
__lshift__ = __lt__ | |
class New2(object): | |
def __init__(self, t): | |
super(New2, self).__init__() | |
self._type = t | |
def __gt__(self, v): | |
self.obj = v | |
if isinstance(v, tuple): | |
return self._type(*v) | |
else: | |
return self._type(v) | |
__rshift__ = __gt__ | |
new = New() | |
class A(object): | |
def __init__(self, v): | |
self.v = v | |
def out(self): | |
print(self.v) | |
x = new<<A>>(123) | |
x.out() | |
print(type(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment