views:

233

answers:

5

If I have a python function like so:

def some_func(arg1, arg2, arg3=1, arg4=2):

Is there a way to determine which arguments were passed by keyword from inside the function?

EDIT

For those asking why I need this, I have no real reason, it came up in a conversation and curiosity got the better of me.

+1  A: 

do you want to know whether arg3 was 1 because it was passed from outside or because it was a default value? No there is no way to do this as far as I'm aware. The main reason, I suspect, that there is no need for such knowledge. What typically is done is the following:

>>> def func(a, b=None):
    if b is None:
# here we know that function was called as:
# func('spam') or func('spam', None) or func('spam', b=None) or func(a='spam', b=None)

        b = 42
SilentGhost
+9  A: 

No, there is no way to do it in Python code with this signature -- if you need this information, you need to change the function's signature.

If you look at the Python C API, you'll see that the actual way arguments are passed to a normal Python function is always as a tuple plus a dict -- i.e., the way that's a direct reflection of a signature of *args, **kwargs. That tuple and dict are then parsed into specific positional args and ones that are named in the signature even though they were passed by name, and the *a and **kw, if present, only take the "overflow" from that parsing, if any -- only at this point does your Python code get control, and by then the information you're requesting (how were the various args passed) is not around any more.

To get the information you requested, therefore, change the signature to *a, **kw and do your own parsing/validation -- this is going "from the egg to the omelette", i.e. a certain amount of work but certainly feasible, while what you're looking for would be going "from the omelette back to the egg"... simply not feasible;-).

Alex Martelli
+1: interesting analogy!
jldupont
Not really, it made me hungry :(
gorsky
+1  A: 

Just do it like this:

def some_func ( arg1, arg2, arg3=None, arg4=None ):
    if arg3 is None:
        arg3 = 1 # default value
    if arg4 is None:
        arg4 = 2 # default value

    # do something

That way you can see when something was set, and you are also able to work with more complex default structures (like lists) without running into problems like these:

>>> def test( arg=[] ):
        arg.append( 1 )
        print( arg )
>>> test()
[1]
>>> test()
[1, 1]
poke
But you still can't tell the difference between `some_func(1, 2, None, 4)` and `some_func(1, 2, arg4=4)`, for example.
Mike DeSimone
Why would you need that? Named arguments are so that you can leave other optional parameters out (so you don't have to write all of them down).
poke
Well, that is the question, isn't it? Mark's the only person who can answer that.
Mike DeSimone
+1  A: 

You're pretty much going to have to redefine your function:

def some_func(*args, **kwargs):

and do the marshaling yourself. There's no way to tell the difference between pass-by-position, pass-by-keyword, and default.

Mike DeSimone
+3  A: 

Here's my solution via decorators:

def showargs(function):
    def inner(*args, **kwargs):
        return function((args, kwargs), *args, **kwargs)
    return inner

@showargs
def some_func(info, arg1, arg2, arg3=1, arg4=2):
    print arg1,arg2,arg3,arg4
    return info

In [226]: some_func(1,2,3, arg4=4)
1 2 3 4
Out[226]: ((1, 2, 3), {'arg4': 4})

There may be a way to clean this up further, but this seems minimally intrusive to me and requires no change to the calling code.

Edit: To actually test if particular args were passed by keyword, then do something like the following inside of some_func:

args, kwargs = info
if 'arg4' in kwargs:
    print "arg4 passed as keyword argument"

Disclaimer: you should probably consider whether or not you really care how the arguments were passed. This whole approach may be unnecessary.

awesomo