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


Messages In This Thread
local/global variables in functions - by abccba - Apr-08-2020, 01:49 PM
RE: local/global variables in functions - by abccba - Apr-08-2020, 02:14 PM
RE: local/global variables in functions - by abccba - Apr-08-2020, 02:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,166 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,121 May-12-2023, 04:16 PM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,787 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,028 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,149 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,155 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Clarity on global variables JonWayn 2 947 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  using variables with functions imported from different files. Scordomaniac 3 1,278 May-24-2022, 10:53 AM
Last Post: deanhystad
  Storing whole functions in variables dedesssse 3 2,093 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Getting parent variables in nested functions wallgraffiti 1 2,146 Jan-30-2021, 03:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

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