Colab

def test_variadic_func(*args):
    print(type(args), len(args), args)
    
test_variadic_func()
test_variadic_func("one")
test_variadic_func("one", "two")
test_variadic_func("one", "two", "three", "four")

<class 'tuple'> 0 ()
<class 'tuple'> 1 ('one',)
<class 'tuple'> 2 ('one', 'two')
<class 'tuple'> 4 ('one', 'two', 'three', 'four')
# packing
args = "one", "two", "three", "four"
print(args)

# unpacking
one, two, three, four = args
print(one,two,three, four)

('one', 'two', 'three', 'four')
one two three four
def myprint1(*args):
    print(*args) # unpacking
    # print(args[0], args[1], args[2], ... args[-1])

print("one", "two", "three")
myprint1("one", "two", "three")

one two three
one two three
import sys
def myprint2(*args, sep=" ", end="\n", file=sys.stdout):
    print(*args, sep=sep, end=end, file=file) # unpacking
    # print(args[0], args[1], args[2], ... args[-1])
    
print("one", "two", "three", sep=" @ ", end=" @\n")
myprint2("one", "two", "three", sep=" @ ", end=" @\n")

one @ two @ three @
one @ two @ three @
def myprint2(*args, **kwargs):
    print(*args, **kwargs)
    
myprint2(1,2,3, sep=" @ ")
# myprint2(1,2,3, magic=" @ ")
    

1 @ 2 @ 3

    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-14-09f73822af3f> in <module>
          3 
          4 myprint2(1,2,3, sep=" @ ")
    ----> 5 myprint2(1,2,3, magic=" @ ")
          6 


    <ipython-input-14-09f73822af3f> in myprint2(*args, **kwargs)
          1 def myprint2(*args, **kwargs):
    ----> 2     print(*args, **kwargs)
          3 
          4 myprint2(1,2,3, sep=" @ ")
          5 myprint2(1,2,3, magic=" @ ")


    TypeError: 'magic' is an invalid keyword argument for print()


def test_variadic_func2(*args, **kwargs):
    print(type(args), len(args), args)
    print(type(kwargs), len(kwargs), kwargs)
    
test_variadic_func2(1,2,3, magic=" @ ", blah="blah")

<class 'tuple'> 3 (1, 2, 3)
<class 'dict'> 2 {'magic': ' @ ', 'blah': 'blah'}
def very_important_function(a,b,c):
    print("very important", a, b, c)

very_important_function(10, 20, 30)

very important 10 20 30
def make_debug_function(func):
    def run(*args, **kwargs):
        print(func.__name__, " is about to run with", *args, **kwargs)
        result = func(*args, **kwargs)
        print(func.__name__, " finished running, returning", result)
        return result
    
    return run

very_important_function(10, 20, 30)
print()

debug_very_important_function = make_debug_function(very_important_function)
debug_very_important_function(10, 20, 30)
print()

very_important_function = debug_very_important_function
very_important_function(10, 20, 30)

very important 10 20 30

very_important_function  is about to run with 10 20 30
very important 10 20 30
very_important_function  finished running, returning None

very_important_function  is about to run with 10 20 30
very important 10 20 30
very_important_function  finished running, returning None