Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. class Bar:
  2. def __init__(self, x):
  3. self.x = x
  4.  
  5. barob = Bar(6)
  6.  
  7. def foobar():
  8. barob = Bar(3) #local scope, the barob in global namespace is not affected.
  9.  
  10. foobar()
  11. print(barob.x) #prints 6
  12.  
  13. def foobar2():
  14. global barob #barob is the one in global space
  15. barob = Bar(3)
  16.  
  17. foobar2()
  18. print(barob.x) #prints 3
  19.  
  20. barob = Bar(5)
  21. def foobar3():
  22. barob.x = 7
  23.  
  24. foobar3()
  25. print(barob.x) #prints 7. Why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement