Running a file with global variables

So this is a piece of code:

global episode = "Episode404"
import testing
reload (testing)
#or
python testing.py

testing.py:

def doIt():
    print episode
doIt()

And it throws me

# Error: invalid syntax # 

I assume this is because I'm trying to pass a global variable and run? How can i fix this?

+4
source share
2 answers

Invalid line below:

global episode = "Episode404"

But you also do not understand the concept of a global team. You must use it to change the value of a variable defined outside the scope of the current work.

That Andy answered the works, but this is not necessary, since you can do it with the same result:

episode = "Episode404"

def doIt():
    print(episode)

doIt()

global , doIt(), doIt() :

episode = "Episode404"

def doIt():
    global episode
    print(episode)
    episode = "New Episode"

doIt()
print(episode)

:

"Episode404"
"New Episode"

, doIt()?

test.py

def doIt(episode):
    print(episode)

:

from testing import doIt

episode = "Episode404"
doIt(episode)

, , . , , , , .

+2

, Python , . :

import testing

global episode
episode = "Episode404"

def doIt():
    print(episode)

doIt()

, Python.

0

Source: https://habr.com/ru/post/1682126/


All Articles