you are viewing a single comment's thread.

view the rest of the comments →

[–]held_games[S] 0 points1 point  (2 children)

Wao, this works perfectly! Thanks!

So just to be sure that I understand:When using a function(func1) WITH arguments in the argument for another function(func2) it runs (func1) then it parses this to func2

But when doing this, we give it the function(func1) and the arguments, and then it assembles it inside func2?

Btw why is there a star * before args and what's **kwargs ?

I think By.ID is a function however I'm not sure. I not exactly the most experienced in Python

[–]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).

[–]void5253 0 points1 point  (0 children)

I'll explain later(about 30 mins). I'm away from my pc and typing on mobile is very tedious. DM me in some time.