0

I am trying out Python for a new project, and in that state, need some global variable. To test without changing the hardcoded value, I've tried to make a function that reassign thoses variables:

foo = None;

def setFoo(bar):
    global foo = bar;

setFoo(1);

However, this get:

  File ".\test.py", line 4
    global foo = bar;
               ^
SyntaxError: invalid syntax

I've already read two questions higly related to my problem, but obviously missed some gotcha of Python.

Could anyone enlighten me?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • 1
    `global foo` and `foo = bar` on separate lines. – Moses Koledoye Sep 22 '16 at 09:39
  • As a side note, you do not need to use ';' to end the line. Python does not need them. – Teemu Risikko Sep 22 '16 at 09:41
  • Well, that was a simple gotcha. – DrakaSAN Sep 22 '16 at 09:41
  • Possible duplicate of [Python function global variables?](http://stackoverflow.com/questions/10588317/python-function-global-variables) – khelwood Sep 22 '16 at 09:42
  • @TeemuRisikko: I know, but coming from C, I use them whenever I can, even in JS. It's sort of a reflex, and don't hurt the code. – DrakaSAN Sep 22 '16 at 09:42
  • Everything has been said, except maybe that global variable are a bad practice in general. If you have a bunch of methods and want to share variables globally, why not. But I would structure the code with classes and avoid using globvars ;) – Floran Gmehlin Sep 22 '16 at 09:44
  • @khelwood: Could be, what I was missing was that `global` was a statement on its own. – DrakaSAN Sep 22 '16 at 09:45
  • @DrakaSAN Yep, but there are answers in those questions that clearly state the correct syntax. – khelwood Sep 22 '16 at 09:46
  • 1
    You should write Python as Python, not as C. Semicolons *are* required in JS, it is just that they are inferred where not present. That is not true of Python; you should not use them. – Daniel Roseman Sep 22 '16 at 09:47

5 Answers5

2

global and assignment are both statements. You can't mix them on the same line.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2
foo = None;

def setFoo(bar):
    global foo 
    foo = bar

setFoo(1);
saurabh baid
  • 1,819
  • 1
  • 14
  • 26
0

As saurabh baid pointed out, the correct syntax is

foo = None

def set_foo(bar):
    global foo 
    foo = bar

set_foo(1)

Using the global keyword allows the function to access the global scope (i.e. the scope of the module)

Also notice how I renamed the variables to be snake_case and removed the semi-colons. semi-colons are unnecessary in Python, and functions are typically snake_case :)

Joe Reynolds
  • 191
  • 1
  • 2
  • 10
  • "shouldn't be used" Why? From what I read, they are unneccessary, but don't hurt readability nor code quality. – DrakaSAN Sep 22 '16 at 09:44
  • @DrakaSAN, it's generally seen as unPythonic, though I suppose there are reasons to use them in rare cases like multiple statements on one line, but personally I can't see why you'd add more noise to a program – Joe Reynolds Sep 22 '16 at 09:45
  • @DrakaSAN They certainly do hurt the readability, by introducing redundant punctuation in a way not consistent with the expected use of the language. Don't try and write Python like it's C. – khelwood Sep 22 '16 at 09:49
0

global and foo = bar on separate lines

foo = None

def setFoo(bar):
    global foo
    foo = bar

setFoo(1)

don't use semicolon: Python: What Does a Semi Colon Do?

Community
  • 1
  • 1
over.unity
  • 315
  • 1
  • 8
0

Python doesn't require you to explicitly declare variables and automatically assumes that a variable that you assign has function scope unless you tell it otherwise. The global keyword is the means that is provided to tell it otherwise. And you can't assign value when you declare it as global. Correct way will be

foo = None

def setFoo(bar):
    global foo 
    foo = bar

setFoo(1)
RejeeshChandran
  • 4,168
  • 3
  • 21
  • 32