Colab

def varfunc(*args):
    print(f"type={type(args)}, len={len(args)}")
    print(args)
    

                  

varfunc()

type=<class 'tuple'>, len=0
()
varfunc(1,2,3)

type=<class 'tuple'>, len=3
(1, 2, 3)
varfunc("h", "e", "l", "l", "o")

type=<class 'tuple'>, len=5
('h', 'e', 'l', 'l', 'o')
def varfunc2(a,b,c, *args):
    print(a,b,c)
    print(f"type={type(args)}, len={len(args)}")
    print(args)

varfunc2()


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

    TypeError                                 Traceback (most recent call last)

    <ipython-input-6-1062b2caca7b> in <module>
    ----> 1 varfunc2()
    

    TypeError: varfunc2() missing 3 required positional arguments: 'a', 'b', and 'c'


varfunc2(1,2,3)

1 2 3
type=<class 'tuple'>, len=0
()
varfunc2(1,2,3, 4, 5, 6)

1 2 3
type=<class 'tuple'>, len=3
(4, 5, 6)
varfunc(end="\n")


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

    TypeError                                 Traceback (most recent call last)

    <ipython-input-9-e70da5c7e22a> in <module>
    ----> 1 varfunc(end="\n")
    

    TypeError: varfunc() got an unexpected keyword argument 'end'


print(1,2,3, sep =" @ ", end=" !!! \n")

1 @ 2 @ 3 !!! 
def myprint(*args, sep=' ', end='\n'):
    print(*args, sep=sep, end=end)

myprint("hello", "world", "nice", "to", "meet", "you")

hello world nice to meet you
print('hello')

hello
myprint(blah="blah")


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

    TypeError                                 Traceback (most recent call last)

    <ipython-input-22-3095fd74f9d2> in <module>
    ----> 1 myprint(blah="blah")
    

    TypeError: myprint() got an unexpected keyword argument 'blah'


myprint(just_invented_this="blah")


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

    TypeError                                 Traceback (most recent call last)

    <ipython-input-24-d660da8a1e87> in <module>
    ----> 1 myprint(just_invented_this="blah")
    

    TypeError: myprint() got an unexpected keyword argument 'just_invented_this'


dict(a=1, b=2, blah=10, testthis=100)

{'a': 1, 'b': 2, 'blah': 10, 'testthis': 100}
def mydict(**kwargs):
    print(f"type={type(kwargs)}, len={len(kwargs)}")
    print(kwargs)
    #return kwargs
    return dict(**kwargs)

mydict(a=1, b=2, blah=10, testthis=100)

type=<class 'dict'>, len=4
{'a': 1, 'b': 2, 'blah': 10, 'testthis': 100}
mydict(
    param1 = "I",
    p2 = "can",
    p3 = "send",
    p4 = "anything",
    whynot = "!"
)
    

type=<class 'dict'>, len=5
{'param1': 'I', 'p2': 'can', 'p3': 'send', 'p4': 'anything', 'whynot': '!'}
{'param1': 'I', 'p2': 'can', 'p3': 'send', 'p4': 'anything', 'whynot': '!'}
x = mydict(
    param1 = "I",
    p2 = "can",
    p3 = "send",
    p4 = "anything",
    whynot = "!"
) 

type=<class 'dict'>, len=5
{'param1': 'I', 'p2': 'can', 'p3': 'send', 'p4': 'anything', 'whynot': '!'}
print(x)

{'param1': 'I', 'p2': 'can', 'p3': 'send', 'p4': 'anything', 'whynot': '!'}
def full_variadic(*args, **kwargs):
    print(args)
    print(kwargs)
    

full_variadic(1,2,3, name='avi', lname="blah")

(1, 2, 3)
{'name': 'avi', 'lname': 'blah'}
def make_wrapper(func):
    
    def the_wrapper(*args, **kwargs):
        print("I'm wrapping the func", func.__name__)
        print(args)
        print(kwargs)
        x = func(*args, **kwargs)
        print("returning", x)
        return x
    
    return the_wrapper


myprint = make_wrapper(print)

myprint(1,2,3, end=" @@@\n")

I'm wrapping the func print
(1, 2, 3)
{'end': ' @@@\n'}
1 2 3 @@@
returning None
import math

mysin = make_wrapper(math.sin)

mysin(0)

I'm wrapping the func sin
(0,)
{}
returning 0.0
0.0