Advertisement
joseville

Python - mutable "static" variable

Oct 14th, 2021 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.28 KB | None | 0 0
  1. class Test:
  2.     i = [3]
  3.  
  4. t = Test()
  5. print(t.i) # [3]
  6.  
  7. t.i[0] = 5
  8. print(Test.i) # [5]
  9. print(t.i) # [5]
  10.  
  11. Test.i[0] = 6
  12. print(t.i) # [6]
  13.  
  14. print(Test.i) # [6]
  15.  
  16. u = Test()
  17. print(u.i) # [6]
  18.  
  19. print(Test.__dict__) # {'i': [6], ...}
  20. print(t.__dict__) # {}
  21. print(u.__dict__) # {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement