user497457
user497457

Reputation: 327

declaring a global dynamic variable in python

I'm a python/programming newbie and maybe my question has no sense at all.

My problem is that I can't get a variable to be global if it is dynamic, I mean I can do this:

def creatingShotInstance():

    import movieClass

    BrokenCristals = movieClass.shot()
    global BrokenCristals #here I declare BrokenCristals like a global variable and it works, I have access to this variable (that is a  shot class instance) from any part of my script.
    BrokenCristals.set_name('BrokenCristals')
    BrokenCristals.set_description('Both characters goes through a big glass\nand break it')
    BrokenCristals.set_length(500)
    Fight._shots.append(BrokenCristals)

def accesingShotInstance():
    import movieClass

    return BrokenCristals.get_name()#it returns me 'BrokenCristals'

but if instead of doing that I declare a string variable like this:

def creatingShotInstance():

    import movieClass

    a = 'BrokenCristals'

    vars()[a] = movieClass.shot()
    global a #this line is the only line that is not working now, I do not have acces to BrokenCristals class instance from other method, but I do have in the same method.
    eval(a+".set_name('"+a+"')")
    eval(a+".set_description('Both characters goes through a big glass\nand break it')")
    eval(a+".set_length(500)")
    Fight._shots.append(vars()[a])

def accesingShotInstance():
    import movieClass

    return BrokenCristals.get_name()#it returns me 'BrokenCristals is not defined'

I tried this :

global vars()[a]

and this:

global eval(a)

but It gives me an error. What should I do?

Upvotes: 8

Views: 14774

Answers (4)

mOmOney
mOmOney

Reputation: 373

Your code works fine!

It was just a spelling mistake and missing brackets!

First syntax error there are no square brackets:

  File "main.py", line 6
    global BrokenCristals
    ^
SyntaxError: name 'BrokenCristals' is assigned to before global declaration

Second syntax error there is no letter s on the end of global:

  File "main.py", line 6
    global [BrokenCristals]
           ^
SyntaxError: invalid syntax

Corrected syntax:

def creatingShotInstance():

    import movieClass

    BrokenCristals = movieClass.shot()
    globals [BrokenCristals]
    BrokenCristals.set_name('BrokenCristals')
    BrokenCristals.set_description('Both characters goes through a big glass\nand break it')
    BrokenCristals.set_length(500)
    Fight._shots.append(BrokenCristals)

def accesingShotInstance():
    import movieClass

    return BrokenCristals.get_name()

Corrected syntax using declared string:

def creatingShotInstance():

    import movieClass

    a = 'BrokenCristals'

    vars()[a] = movieClass.shot()
    globals [a] 
    eval(a+".set_name('"+a+"')")
    eval(a+".set_description('Both characters goes through a big glass\nand break it')")
    eval(a+".set_length(500)")
    Fight._shots.append(vars()[a])

def accesingShotInstance():
    import movieClass

    return BrokenCristals.get_name()

Upvotes: 0

Katriel
Katriel

Reputation: 123772

For completeness, here's the answer to your original question. But it's almost certainly not what you meant to do -- there are very few cases where modifying the scope's dict is the right thing to do.

globals()[a] = 'whatever'

Upvotes: 19

Ned Batchelder
Ned Batchelder

Reputation: 375942

Instead of a dynamic global variable, use a dict:

movies = {}

a = 'BrokenCristals'

movies[a] = movieClass.shot()
movies[a].set_name(a)
# etc

Upvotes: 8

robert
robert

Reputation: 34438

The global keyword specifies that a variable you're using in one scope actually belongs to the outer scope. Since you do not have nested scopes in your example, global doesn't know what you're trying to do. See Using global variables in a function other than the one that created them

Upvotes: 2

Related Questions