Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RecursiveCatcher:
- def __init__(self):
- global stack_depth
- stack_depth = 0
- self.no_of_getattribute_calls = 0
- def __getattribute__(self, attr_name):
- # We need something that is outside the scope of this class:
- global stack_depth
- stack_depth += 1
- if stack_depth<=10: # to prevent a stack overflow
- self.no_of_getattribute_calls += 1
- # Oops! We just accessed an attribute (no_of_getattribute_calls)
- # Guess what happens when self.no_of_getattribute_calls is
- # accessed?
- # Using 'object' directly because using super() here will also
- # trigger a __getattribute__() call.
- return object.__getattribute__(self, attr_name)
- def my_method(self):
- pass
- def test_getattribute_is_a_bit_overzealous_sometimes(self):
- catcher = self.RecursiveCatcher()
- catcher.my_method()
- global stack_depth
- self.assertEqual(11, stack_depth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement