Python Forum
local/global variables in functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local/global variables in functions
#1
x = 1


def f1(y):
    y += 1
    return y


x1 = f1(x)
print('x = ', x, '  x1 = ', x1)
answer:
x = 1 x1 = 2

x_list = ['1','2']


def f2(y_list):
    y_list[0] = '0'
    return y_list


x1_list = f2(x_list)
print('x_list = ', x_list, '  x1_list = ', x1_list)
answer:
x_list = ['0', '2'] x1_list = ['0', '2']

why in the first case the global variable is preserved and not in the second case?
Reply
#2
This isn't a matter of global versus local. Rather, it's a matter of Python's sequence implementation. Sequences, such as a list, store values by reference to a memory location. As a result, a list is basically a pointer used in other languages. This enables changes to a list made within a function to change the list outside the function.
Reply
#3
(Apr-08-2020, 01:59 PM)stullis Wrote: This isn't a matter of global versus local. Rather, it's a matter of Python's sequence implementation. Sequences, such as a list, store values by reference to a memory location. As a result, a list is basically a pointer used in other languages. This enables changes to a list made within a function to change the list outside the function.

variables also use a pointer to a certian memory location. why should there be a different approach in both cases?
though more than discussing the philosofy around it, I would prefer to know the way around and understand if there is a 'clean' way to preserve the content of lists, when passing them to a function.
Reply
#4
Lists are mutable, integers are not. If you want an immutable sequence, use a tuple.
Reply
#5
it is a nice suggestion but does not solve the problem I was addressing.
The list is created while the program is running.
Is there away to creat a new list that uses another position in memory?

ok, it can be converted. Fine!
Reply
#6
You can make a copy of the list in your function
def f2(y_list):
    new_list = list(y_list)
    new_list[0] = '0'
    return new_list
Reply
#7
A nice video about this from PyCon: https://www.youtube.com/watch?v=_AEJHKGk9ns
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Storing whole functions in variables dedesssse 3 335 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Getting parent variables in nested functions wallgraffiti 1 560 Jan-30-2021, 03:53 PM
Last Post: buran
  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
  module to store functions/variables and how to call them? mstichler 3 913 Jun-03-2020, 06:49 PM
Last Post: mstichler
  local / global lists RedWuff 1 717 May-26-2020, 03:11 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 1,879 Apr-12-2020, 03:58 PM
Last Post: TomToad
  Python 2.7 passing variables from functions zetto33 1 685 Mar-19-2020, 07:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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