Skip to content

Instantly share code, notes, and snippets.

@aynik
Created May 2, 2020 02:57
Show Gist options
  • Save aynik/56f9869c883e13da44e1a6dc1ec3a5d2 to your computer and use it in GitHub Desktop.
Save aynik/56f9869c883e13da44e1a6dc1ec3a5d2 to your computer and use it in GitHub Desktop.
class FieldRoot(object):
def __init__(self, __id__, __parent_repr__=lambda x: x):
self.__id__ = __id__
self.__parent_repr__ = __parent_repr__
def __repr__(self):
return self.__parent_repr__(self.__id__)
def __embed__(self, child, __id__):
return child(__id__,
__parent_repr__=lambda x: self.__parent_repr__(
self.__id__ + '{' + x + '}'))
class B(FieldRoot):
@property
def a(self):
return self.__embed__(A, 'a')
class A(FieldRoot):
@property
def b(self):
return self.__embed__(B, 'b')
a = A('a')
# a.b.a.b.a.b.a.b = 'a{b{a{b{a{b{a{b}}}}}}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment