Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over function arguments

I have a Python function accepting several string arguments def foo(a, b, c): and concatenating them in a string. I want to iterate over all function arguments to check they are not None. How it can be done? Is there a quick way to convert None to ""?

Thanks.

like image 974
jackhab Avatar asked May 26 '10 11:05

jackhab


People also ask

How do you iterate a function over a list?

We can iterate through a list by using the range() function and passing the length of the list. It will return the index from 0 till the end of the list.

How do you iterate a function in Python?

You can create an iterator object by applying the iter() built-in function to an iterable. You can use an iterator to manually loop over the iterable it came from. A repeated passing of iterator to the built-in function next() returns successive items in the stream.

Is it possible to pass multiple arguments to a function?

We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.

How many arguments can for loop take?

With two arguments in the range function, the sequence starts at the first value and ends one before the second argument. Programmers use x or i as a stepper variable.


4 Answers

locals() may be your friend here if you call it first thing in your function.

Example 1:

>>> def fun(a, b, c):
...     d = locals()
...     e = d
...     print e
...     print locals()
... 
>>> fun(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'e': {...}, 'd': {...}}

Example 2:

>>> def nones(a, b, c, d):
...     arguments = locals()
...     print 'The following arguments are not None: ', ', '.join(k for k, v in arguments.items() if v is not None)
... 
>>> nones("Something", None, 'N', False)
The following arguments are not None:  a, c, d

Answer:

>>> def foo(a, b, c):
...     return ''.join(v for v in locals().values() if v is not None)
... 
>>> foo('Cleese', 'Palin', None)
'CleesePalin'

Update:

'Example 1' highlights that we may have some extra work to do if the order of your arguments is important as the dict returned by locals() (or vars()) is unordered. The function above also doesn't deal with numbers very gracefully. So here are a couple of refinements:

>>> def foo(a, b, c):
...     arguments = locals()
...     return ''.join(str(arguments[k]) for k in sorted(arguments.keys()) if arguments[k] is not None)
... 
>>> foo(None, 'Antioch', 3)
'Antioch3'
like image 51
Johnsyweb Avatar answered Oct 17 '22 06:10

Johnsyweb


def func(*args):
    ' '.join(i if i is not None else '' for i in args)

if you're joining on an empty string, you could just do ''.join(i for i in args if i is not None)

like image 29
SilentGhost Avatar answered Oct 17 '22 05:10

SilentGhost


You can use the inspect module and define a function like that:

import inspect
def f(a,b,c):
    argspec=inspect.getargvalues(inspect.currentframe())
    return argspec
f(1,2,3)
ArgInfo(args=['a', 'b', 'c'], varargs=None, keywords=None, locals={'a': 1, 'c': 3, 'b': 2})

in argspec there are all the info you need to perform any operation with argument passed.

To concatenate the string is sufficient to use the arg info received:

def f(a,b,c):
    argspec=inspect.getargvalues(inspect.currentframe())
    return ''.join(argspec.locals[arg] for arg in argspec.args)

For reference: http://docs.python.org/library/inspect.html#inspect.getargvalues

like image 4
pygabriel Avatar answered Oct 17 '22 05:10

pygabriel


Is this perhaps what you'd like?

def foo(a, b, c):
    "SilentGhost suggested the join"
    ' '.join(i if i is not None else '' for i in vars().values())

def bar(a,b,c): 
    "A very usefull method!"
    print vars()
    print vars().values()

Notice the use of vars(), which returns a dict.

like image 2
extraneon Avatar answered Oct 17 '22 07:10

extraneon