Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scope of variable confusion
#2
The global keyword tells Python to look for the following symbols in the global scope instead of the local scope. Placing "global check" at the top of the file does not do anything at all because you are currently in the global scope.

global only makes sense inside a function. Here the "global check" tells Python that the "check" inside this function is the global variable "check".
def sample_fcn()
    global check  # The global has to be inside the function
    check += 1
Reply


Messages In This Thread
Scope of variable confusion - by Mark17 - Feb-23-2022, 09:47 PM
RE: Scope of variable confusion - by deanhystad - Feb-23-2022, 09:55 PM
RE: Scope of variable confusion - by Mark17 - Feb-23-2022, 11:04 PM
RE: Scope of variable confusion - by ThomasL - Feb-24-2022, 10:53 AM
RE: Scope of variable confusion - by Mark17 - Feb-24-2022, 12:50 PM
RE: Scope of variable confusion - by deanhystad - Feb-24-2022, 02:27 PM
RE: Scope of variable confusion - by Mark17 - Feb-24-2022, 02:51 PM
RE: Scope of variable confusion - by ibreeden - Feb-24-2022, 03:09 PM
RE: Scope of variable confusion - by deanhystad - Feb-24-2022, 03:29 PM
RE: Scope of variable confusion - by Mark17 - Feb-24-2022, 03:51 PM
RE: Scope of variable confusion - by deanhystad - Feb-24-2022, 06:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 8,841 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 1,689 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Variable scope issue melvin13 2 2,366 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 4,428 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 2,714 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 4,944 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 4,901 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Solving a scope issue profconn1 4 3,605 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Namespace and scope difference Uchikago 9 6,511 Jul-03-2019, 03:36 PM
Last Post: Uchikago
  what is scope for python? samexpert 2 2,934 Jun-24-2019, 01:03 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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