I have following code:
#!/usr/bin/python
import sys
import os
from pprint import pprint as pp
def test_var_args(farg, default=1, *args, **kwargs):
print "type of args is", type(args)
print "type of args is", type(kwargs)
print "formal arg:", farg
print "default arg:", default
for arg in args:
print "another arg:", arg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])
print "last argument from args:", args[-1]
test_var_args(1, "two", 3, 4, myarg2="two", myarg3=3)
Above code outputs:
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4
As you can see as a default argument is passed "two". But I do not want to assign to the default variable anything unless I say it explicitly. In other words, I want that aforementioned command returns this:
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: 1
another arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4
Changing the default variable should be done explicitly, e.g. using something like this (following command gives compilation error, it was just my attempt)
test_var_args(1, default="two", 3, 4, myarg2="two", myarg3=3)
:
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4
I have tried following but it also returns an compilation error:
test_var_args(1,, 3, 4, myarg2="two", myarg3=3)
Is this possible?