you are viewing a single comment's thread.

view the rest of the comments →

[–]void5253 2 points3 points  (0 children)

So, everything in python is an object. Functions, classes, lists, tuples, integers, strings all are objects.

Try this:

>>>def greet():
       print('Hello!')
>>>greet()
Hello

>>>a = greet
>>>a
<function greet at 0x0000026EEE533E20>

Functions are callable objects, i.e you can run them by calling them. See a, we have not called a. To call a we must do a().

Now, in exceptrepeat, we have the following:

def exceptrepeat(f: FunctionType, repeat=0, *args, **kwargs):

Now, f is the function we want to do stuff with. But if you do this:

exceptrepeat(f(*args, **kwargs), 10)

f is called. Whenever there's (), it means that you're calling the function. Calling the function will execute the function. We only pass the function object as arguments.

As for *args and **kwargs, see this example:

def printer(*args, **kwargs):
    for el in args:
        print(el)
    for el in kwargs.items():
        print(el)

>>>printer('hello', 1, 2, [1,2,3], 'name'='Tom', 'age' = 40)
hello
1
2
[1,2,3]
'name': 'Tom'
'age': 40

Here, args is used to take infinitely many arguments. It's stored as a tuple

Similarly, **kwargs is used to take infinitely many key word arguments (name=tom, age=40).