Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable scope issue
#1
Hello,
here's a simplified version of what's causing me trouble :
x = int

def init(): # 
    x = 0
    return

def f():
    x += 1
    return

init()
f()
f()
print(x)
Error:
8: Local variable 'x' defined in enclosing scope on line 1 referenced before assignement
What does this error mean in this context ? Aren't variables defined outside of functions set to global by default ?
Thank you !
Reply
#2
No. Variables on the left hand side of the = operator are local by default.
Reply
#3
Thank you !
Reply


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 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 795 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,737 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope - "global x" didn't work... ptrivino 5 2,978 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,775 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 3,337 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,432 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Variable Issue slackerman73 2 1,879 Nov-09-2019, 04:34 PM
Last Post: slackerman73
  Solving a scope issue profconn1 4 2,507 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Issue with affecting numbers to a variable Adem 3 2,247 Sep-24-2019, 07:48 AM
Last Post: Adem

Forum Jump:

User Panel Messages

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