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
  Global variables not working hobbyist 9 1,115 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 692 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 1,075 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 915 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  local / global lists RedWuff 1 717 May-26-2020, 03:11 AM
Last Post: deanhystad
  local/global variables in functions abccba 6 1,225 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Question about naming variables in class methods sShadowSerpent 1 717 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Creating local variables from a string peckjonk 2 903 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 1,177 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 789 Jan-28-2020, 04:33 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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