python - How do I create a function that will create a global (and assign it a value) with a name that contains the name of the argument given to it? -


i'm using python , trying create function @ name of object given argument , (among other things) create globals contain name, example:

object1=someclass(args,kwds) #the object instance of class given name (e.g.object1) def somefunc(some_argument):    global tau_some_argument    global ron_some_argument    #e.t.c. other variables (i assign variables etc)    tau_some_argument=some_value1    ron_some_argument=some_value2    print ron_some_argument    print tau_some_argument 

now want happen if call function follows: when call

 somefunc(object1) 

i create globals named tau_object1, ron_object1 , assign them values (the print statements irrelevant), if called

somefunc(object2) 

where object2 instance of class create globals: tau_object2, ron_object2, @ moment function creates globals named tau_some_argument (as obvious code).

now read somewhere names of python objects immutable, unless class or function don't know how this, reading feeling may need use meta-classes feels it's overly complicated way of doing should relatively easy.

thanks in advance.

it sounds terrible idea...

def func(prefix):     g = globals()     g[prefix + '_tomato'] = 123     g[prefix + '_broccoli'] = 456 

whatever doing, can done dictionaries, arrays, , other objects in way isn't fragile above method.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -