Created
February 3, 2022 00:24
-
-
Save JIElite/3d346467d0ed467f8cec6f7bce56f7b3 to your computer and use it in GitHub Desktop.
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
| import pprint | |
| def repr(self): | |
| keys = [attr for attr in dir(self) if attr[0] != '_'] | |
| objs = {key:getattr(self, key) for key in keys} | |
| objs['self'] = f'{self.__class__.__name__}({self.val}): 0x{id(self):x}' | |
| return pprint.pformat(objs, depth=2) | |
| class Node: | |
| def __init__(self, val=None, right=None, left=None): | |
| self.val = val | |
| self.right = right | |
| self.left = left | |
| # method binding | |
| Node.__repr__ = repr | |
| n1 = Node(1) | |
| n1.right = Node(2) | |
| n1.left = Node(3) | |
| n1.right.right = Node(4) | |
| n2 = Node(7) | |
| n2.right = Node(8) | |
| n2.a = '@@' | |
| n1.right.left = [Node(5), Node(6), {'test': n2}, [{'test2': Node(11)}]] | |
| obj = dict(eval(n1.__repr__())) | |
| pprint.pprint(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment