Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring a global dynamic variable in python

Tags:

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?

like image 476
user497457 Avatar asked Nov 25 '10 12:11

user497457


People also ask

How do you declare a dynamic variable in Python?

Use a Dictionary to Create a Dynamic Variable Name in Python It is written with curly brackets {} . In addition to this, dictionaries cannot have any duplicates. A dictionary has both a key and value, so it is easy to create a dynamic variable name using dictionaries.

How do you declare a global variable in Python?

We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.

How do you declare a global variable?

You can declare global—that is, nonlocal—variables by declaring them outside of any function definition. It's usually best to put all global declarations near the beginning of the program, before the first function. A variable is recognized only from the point it is declared, to the end of the file.

What is dynamic variable in Python?

A dynamic variable name, sometimes called a variable, is a variable with a name that is the estimation of another variable. Despite the fact that Python is a very dynamic language in which nearly everything is an object, it is possible to construct dynamic variables in Python.


1 Answers

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'
like image 78
Katriel Avatar answered Sep 17 '22 13:09

Katriel