Python and global variables

I could be incorrect on this but I noticed when I define a variable outside a function and to change the variable you need to use the key word ‘global’ but it does not seem to work…

code would look something like,

foo = "foo"
def foo():
    global foo
    foo = "bla"
    self.say(foo)
def bla():
    self.say(foo)
self.say(foo)
foo()
bla()
self.say(foo)

Edit: I found a way around what I needed, just made returns values to and from what I needed…

2 Likes

Ah yeah, it seems our Python parser, Filbert, doesn’t support the global keyword yet. You can check which other slightly-more-advanced Python features are or aren’t yet included on Filbert’s readme: https://github.com/codecombat/filbert

2 Likes

Now I have to code the whole damn thing in Javascript all over again :cry: :pensive: :cry: WHY U DO THIS NICK
Mod edit: Please don’t swear

2 Likes

Is there a solution to global variables in python yet? Or do we have to pass things back and forth?

2 Likes

This thread is over a year old. Try not to revive dead threads, but it’s always good to keep in check in case of updates :slightly_smiling_face:

1 Like

Sorry, I’m new. So you’re saying that it’s better to create a new thread asking the same question than to revive the most relevant thread?

"Ah yeah, it seems our Python parser, Filbert, doesn’t support the global keyword yet. "

Seems like pinging this same topic for context makes the most sense to me.

2 Likes

I suppose in this case it’s okay. For most topics you probably shouldn’t revive dead threads unless you have a related issue or something, but in cases like this it’s always a good idea to check.

1 Like