Python Forum
Question regarding local and global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question regarding local and global variables
#11
(Apr-12-2020, 03:06 PM)TomToad Wrote: But curiously, the ids are overwritten as well which seems to contradict the documentation.
No, it does not contradict the docs, it's the id of the object at which the name points.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
(Apr-12-2020, 03:09 PM)donmerch Wrote: It just stood out to me why one type of object (integer, list) is referred to differently inside and outside a function.
First of all, int and list are different types. There is difference because lists are mutable. Same will apply to other mutable objects like dict.
However, when it comes to assignment, it's not different (your first snippet). You must make distinction when you assign to name c and when you change element INSIDE mutable object (i.e. c[0])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
(Apr-12-2020, 03:11 PM)buran Wrote: No, it does not contradict the docs, it's the id of the object at which the name points.
Yes, it does contradict. According to the docs from here, the id is guaranteed to be constant throught the life of the object.
I would not consider a value that changes to be constant.

Scratch that, sometimes I forget that Python handles objects and types differently from what I'm use to.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,098 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,065 May-12-2023, 04:16 PM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,689 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 985 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,102 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,106 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Clarity on global variables JonWayn 2 905 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,618 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,596 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,128 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020