Guest User

Untitled

a guest
May 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. a = [1,2]; // a has an array
  2. b = &a; // b points to a
  3. *b = 2; // derefernce b to store 2 in a
  4. print(a); // outputs 2
  5. print(*b); // outputs 2
  6.  
  7. class ref:
  8. def __init__(self, obj): self.obj = obj
  9. def get(self): return self.obj
  10. def set(self, obj): self.obj = obj
  11.  
  12. a = ref([1, 2])
  13. b = a
  14. print a.get() # => [1, 2]
  15. print b.get() # => [1, 2]
  16.  
  17. b.set(2)
  18. print a.get() # => 2
  19. print b.get() # => 2
  20.  
  21. func()
  22. {
  23. var a = 1;
  24. var *b = &a;
  25. *b = 2;
  26. assert(a == 2);
  27. }
  28.  
  29. a = [1]
  30. b = a
  31. b[0] = 2
  32. assert a[0] == 2
  33.  
  34. a = [1,2] // a has an array
  35. b = a // b points to a
  36. a = 2 // store 2 in a.
  37. print(a) // outputs 2
  38. print(b) // outputs [1,2]
  39.  
  40. >>> a = [1,2]
  41. >>> id(a)
  42. 28354600
  43.  
  44. >>> b = a
  45. >>> id(a)
  46. 28354600
  47.  
  48. >>> id(b)
  49. 28354600
  50.  
  51. # Change operations like:
  52. b = &a
  53.  
  54. # To:
  55. b = "a"
  56.  
  57. # And change operations like:
  58. *b = 2
  59.  
  60. # To:
  61. locals()[b] = 2
  62.  
  63.  
  64. >>> a = [1,2]
  65. >>> b = "a"
  66. >>> locals()[b] = 2
  67. >>> print(a)
  68. 2
  69. >>> print(locals()[b])
  70. 2
Add Comment
Please, Sign In to add comment