Last active
February 20, 2022 17:06
-
-
Save obestwalter/e82068b95a9576024d0f9504587e24d6 to your computer and use it in GitHub Desktop.
When inspecting self in this way in a property it endlessly recursess
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
import traceback | |
class SomeClass: | |
a_class_attr = "don't find me" | |
def an_instance_method(self): | |
... | |
def _a_private_method(self): | |
... | |
def make_name2callable(obj): | |
n2c = {n: getattr(obj, n) for n in dir(obj)} | |
return {k: v for k, v in n2c.items() if not k.startswith("_") and callable(v)} | |
class ThisWorks(SomeClass): | |
def name2callable(self): | |
return make_name2callable(self) | |
class ThisDoesnt(SomeClass): | |
@property | |
def name2callable(self): | |
return make_name2callable(self) | |
print(ThisWorks().name2callable()) | |
try: | |
print(ThisDoesnt().name2callable) | |
except RecursionError: | |
print("why does this cause a recursion?") | |
traceback.print_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For completeness here is a little test/demo that uses this: