Apr-12-2020, 03:06 PM
(Apr-12-2020, 02:44 PM)donmerch Wrote: So if I do this I see the inside and outside id of a & b are different even though I declared a & b global inside.
That is a good question. If you print the values again after demo() is called, you will see that the main values do change when assigned from within the function. But curiously, the ids are overwritten as well which seems to contradict the documentation.
a = 20 b = 30 c = [1, 2, 3] print("before demo()",id(a),a, id(b),b, id(c),c) def demo(): global a global b a = 21 b = 31 c[0] = 5 print("In demo()",id(a),a, id(b),b, id(c),c) demo() print("After demo()",id(a),a,id(b),b,id(c),c)
Output:before demo() 140724845310816 20 140724845311136 30 1546429675272 [1, 2, 3]
In demo() 140724845310848 21 140724845311168 31 1546429675272 [5, 2, 3]
After demo() 140724845310848 21 140724845311168 31 1546429675272 [5, 2, 3]
Quote:id(object)
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
The output does not show a unique and constant id.