Python Forum
Where to put the global keyword when assigning variables outside a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to put the global keyword when assigning variables outside a function?
#1
Hello, I came across the following code:

a = None

def bind_a_variable():
  global a
  a = []

bind_a_variable()
It is stated that assigning variables outside of the function's scope is possible but those variables must be declared as global via the global keyboard. I am a C programmer. In C, we have to declare a variable first before using it. I would declare global a before
a = None
. However, in the example, a is declared inside a function and after the
a = None 
assignment. Could anybody please clarify?
Reply


Messages In This Thread
Where to put the global keyword when assigning variables outside a function? - by new_to_python - Feb-08-2020, 01:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variables not working hobbyist 9 944 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 555 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Finding global extrema of oscillating function JoeRogan 0 395 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  Global - local variables Motorhomer14 11 863 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  How to make global list inside function CHANKC 6 789 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Do I have to pass 85 variables to function? Milfredo 10 1,166 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 439 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 722 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Issues with storing variables outside of a function cerulean747 7 1,111 Apr-30-2020, 08:46 AM
Last Post: DeaD_EyE
  Assigning variables Godserena 4 712 Apr-26-2020, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

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