Advertisement
martineau

Block Scope

Feb 11th, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. ########################
  2. # foobar.py
  3.  
  4. x = 10
  5.  
  6. class _Foobar:
  7.     @property
  8.     def y(self):
  9.         global x
  10.         return x + 32
  11.  
  12. import sys
  13. _ref, sys.modules[__name__] = sys.modules[__name__], _Foobar()
  14.  
  15. ########################
  16. # test_foobar.py
  17.  
  18. import foobar
  19.  
  20. # Use existing module property.
  21. print(foobar.y)  # -> 42
  22.  
  23. # Dynamically add a property to the module.
  24. foobar.a = 3
  25. foobar.__class__.b = property(lambda self: self.a + 1)
  26. print(foobar.b)  # -> 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement