Pass a variable number of positional arguments and keyword arguments to a function.

The print function accepts a variable number of positional arguments, followed by a variable number of keyword arguments. It’s hard to think of another function that can do that.

#0 positional arguments
print()
print(sep = "")
print(end = "")
print(sep = "", end = "")
print(end = "", sep = "")

#1 positional argument
print(10)
print(10, sep = "")
print(10, end = "")
print(10, sep = "", end = "")
print(10, end = "", sep = "")

#2 positional arguments
print(10, 20)
print(10, 20, sep = "")
print(10, 20, end = "")
print(10, 20, sep = "", end = "")
print(10, 20, end = "", sep = "")

Line 23 prevents us from trying to capitalize the values of the file or flush keyword arguments of print.

variablekeyword.py

The output begins with two empty lines.



N
N
N

ABE
ABE
ABEN
ABEN
ABEN

ABE IKE
ABEXXXIKE
ABE IKEN
ABEXXXIKEN
ABEXXXIKEN

Things to try

  1. Automate the testing. Changes lines 31–50 to
    names = ["abe", "ike", "jake"]
    
    for i in range(len(names) + 1):
        PRINT(*names[:i])
        PRINT(*names[:i], sep = "xxx")
        PRINT(*names[:i], end = "n\n")
        PRINT(*names[:i], sep = "xxx", end = "n\n")
        PRINT(*names[:i], end = "n\n", sep = "xxx")
        print()
    
    
    
    N
    N
    N
    
    ABE
    ABE
    ABEN
    ABEN
    ABEN
    
    ABE IKE
    ABEXXXIKE
    ABE IKEN
    ABEXXXIKEN
    ABEXXXIKEN
    
    ABE IKE JAKE
    ABEXXXIKEXXXJAKE
    ABE IKE JAKEN
    ABEXXXIKEXXXJAKEN
    ABEXXXIKEXXXJAKEN
    
    
  2. Change lines 14–19 to use the following conditional expression.
        ARGS = []
        for arg in args:
            ARGS.append(arg.upper() if isinstance(arg, str) else arg)
    
  3. Change lines 14–19 to the following list comprehension with a conditional expression.
        ARGS = [arg.upper() if isinstance(arg, str) else arg for arg in args]
    
  4. Change lines 21–26 to use the following conditional expression.
        NAMEDARGS = {}
        for key, value in namedArgs.items():
            NAMEDARGS[key] = value.upper() if key == "sep" or key == "end" else value
    
  5. Change lines 21–26 to the following dictionary comprehension.
        NAMEDARGS = {key: value.upper() if key == "sep" or key == "end" else value
            for key, value in namedArgs.items()}