Skip to content

Instantly share code, notes, and snippets.

@adolli
Created October 12, 2019 07:03
Show Gist options
  • Save adolli/77aeb242c0928bf511181a6dbe57b421 to your computer and use it in GitHub Desktop.
Save adolli/77aeb242c0928bf511181a6dbe57b421 to your computer and use it in GitHub Desktop.
Generic types in python
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