1

I have a function with a definite set of arguments, but that during development the amount of arguments it recieves could change. Each argument can either be a string, or a list. I want to someway loop trough the arguments of the function, and if they are a string, convert them to a list with that single element.

I tried using locals() to be able to loop trough the arguments, but I don't know how to modify the value during the iteration, so I guess that's not the correct approach:

def function(a, b, c, d):
    for key in locals():
        if type(key) == str:
            # ??? key = list(locals[key])

    # ... more code over here, assuming that a,b,c,d are lists

What's the correct way to acomplish this?

Thanks!

Zamaroht
  • 150
  • 1
  • 12
  • 5
    use *args or **kwargs – Padraic Cunningham Jan 09 '15 at 18:14
  • I'd rather not use *args because the arguments I recieve in the function are explicit. I could easily do `if type(a)==str: a = list(a)' for each argument, but if the signature of the function changes during development, I want this behaviour to adapt dinamically. – Zamaroht Jan 09 '15 at 18:17
  • You can't "modify the value during iteration" within a function anyway as assignment to the locals dict doesn't update the locals... – mgilson Jan 09 '15 at 18:18
  • Exactly, I don't know which other way I can dinamically transform every str recieved to a list, so I can assume all the variables are lists from there on. – Zamaroht Jan 09 '15 at 18:20

3 Answers3

1

You can use isinstance(key, str) to check if a variable is a str or not

def function(a, b, c, d):
    l= locals()
    for i in l:
        key = l[i]
        if isinstance(key, str):
            l[i] = [l[i]]
    l = l.values()
    print (l)
function('one', 'two', ['bla','bla'], ['ble'])

If you want list, then you need to have a line

 l = l.values()

This will be

[['a'], ['c'], ['b'], ['d']]

EDIT: As you have suggested, the correct program is

def function(a, b, c, d):
    l= locals()
    dirty= False
    for i in l:
        key = l[i]
        if isinstance(key, str):
            l[i] = [l[i]]
            dirty= True
    if dirty:
        function(**l)
    else:
        print a, b, c, d
    print (l)

function('one', 'two', ['bla','bla'], ['ble'])

This will output

['one'], ['bla', 'bla'], ['two'], ['ble']
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • What I want is to actually do `a,b,c,d=l` where the print is, so I can code the rest of the function assuming that all four values are lists, but without explicitly naming `a`, `b`, `c` and `d` in that line. – Zamaroht Jan 09 '15 at 18:28
  • @Zamaroht So you want to access variables as a list? – Bhargav Rao Jan 09 '15 at 18:29
  • I want that if, for example, I call the function with `function('one', 'two', ['bla','bla'], ['ble'])`, after executing some code I can assume during the rest of the function that the values are `['one']`, `['two']`, `['bla','bla']` and `['ble']` for a, b, c and d respectively. – Zamaroht Jan 09 '15 at 18:31
  • What I actually wanted is to set the parameters of a,b,c,d to the values in l. You gave me the idea to make it work with some recursion and a dirty flag, this is the code that works, feel free to include it in the answer: `def function(a, b, c, d): l= locals() dirty=False for i in l: key = l[i] if isinstance(key, str): l[i] = [l[i]] dirty=True if dirty: function(**l) else: print a, b, c, d function('bla', ['ble'], 'me', ['gsa','gs'])` Thanks! – Zamaroht Jan 09 '15 at 18:42
  • @Zamaroht Thanks for the edit, do accept the answer if you fell it is correct – Bhargav Rao Jan 09 '15 at 18:50
1

Use *args or **kwargs:

def use_args(*args):
    # covert strings to `list` containing them
    new_args = map(lambda x: [x] if isinstance(x, str) else x, args)
    return new_args
print use_args([1, 2], "3")

Output:

[[1, 2], ['3']]
Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0
def function(*args):

    arg_list = list(args)
    for i in range(len(arg_list)):
        if type(arg_list[i]) == str:
            #Do the stuff
        elif type(arg_list[i]) == list:
            #Do the stuff
        else:
            #Do the stuff

function ('a','b', ['c', 'd', 'e'])
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
hariK
  • 2,722
  • 13
  • 18