1

I need to create a function which can:

  1. Inspect input arguments and get the number of inputs and their values
  2. Create a loop, and add string '_in' after each input name
  3. Make the new inputs global
  4. Assign new inputs with old values

I tried to use inspect.getargspec() but it seems like this should be called outside the function. So any suggestions?

Here is the pseudo code: Define this function:

def set_global(a1, a2, a3, .... an):
    #the number of arguments is not fixed and we need to detect that
    for old_input in total input:
         new_input_name=str(old_input.name)+'_in'
         global new_input_name 
         new_input_name = old_input.value
2
  • What's your use case? You may want to use a decorator here. Commented Jan 16, 2013 at 16:30
  • @ThomasOrozco: I have updated my post with a broken function. Commented Jan 16, 2013 at 16:44

2 Answers 2

2
def foo(*args, **kwargs):
    print "number of fixed args: %d" % len(args)
    print "number of keyword args: %d" % len(kwargs)
    print "keyword argument names: %s" % str(kwargs.keys())

We can apply this with globals() to do what you want:

def set_globals(**kwargs):
    for argname in kwargs:
        globals()['%s_in' % argname] = kwargs[argname]

So, using the above function from the Python interactive interpreter:

>>> set_globals(foo='x', bar='y')
>>> foo_in
'x'
>>> bar_in
'y'
Sign up to request clarification or add additional context in comments.

7 Comments

Is there a sane way to create local variables dynamically in any language?
@mgilson Funny, funny. What I mean is that the Python standard explicitly disallows all practices which result in dynamic local variable creation. Contrast this to, for example, Perl's $$.
Actually, At the module level, you can modify the dict returned by globals. In many implementations (I think) you can do the same thing with locals, but that one isn't guaranteed. As far as contrasting it with perl, then I would need to learn perl to understand that syntax -- (I think I get it) -- but I'd rather not learn perl ...
@mgilson Using locals that way is unsafe. Which is why I used the word "sane".
Thanks for the reply. I have updated the post, which might explain the question a little bit...
|
1

I really think you're looking for the globals function.

a = 1
globals()['a'] = 2
print a  #2
globals()['a_in'] = 2
print a_in  #2

You could put this in a function:

def do_something_with_globals(**kwargs):
    for k,v in kwargs.items():
        globals()[k+'_in'] = v

Which could be called like this:

a = 1
b = 2
do_something_with_globals(a=a,b=b)
print a_in
print b_in

But, honestly, I really don't think this is a good idea...

1 Comment

Thanks for the help. Actually, this is part of the functions for a unittest. I failed to send external parameters to the unittest. The only solution I came up is to use global variables...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.