Effective Functions Flashcards

1
Q

What does it mean that functions are first class objects?

A

You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the correlation between a function and its name?

A
Totally different
def yell:
   print(blabla)
bark=yell
print(bark.\_\_name\_\_)
>>"yell"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is important the fact that you can pass functions as arguments to other functions?

A

Due to the fact that passing a function means passing a behavior

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a lexical closure?

A

Lexical closure is the closure which remembers its lexical scope even if its not currently at that scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

All functions are objects, but does the reverse hold?

A

No. All objects are not functions but they can made callabe using __call__ method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a lambda keyword?

A

It is a keyword required to create small anonymous functions.
add= lambda x,y:x+y
print(add(32,43))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between lambdas and functions>

A

lambdas do not have a return statement.expression

That’s why some people refer to lambdas as single expression functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the power of the decorator?

A

The power of decorator lies upon the fact that it can extend the behavior of the callable without the need of modifying it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a decorator?

A

A decorator is a callable that takes as argument a function and returns a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What args and kargs arguments?

A

They allow a function to take optional arguments.

*Args expands optional arguments as a tuple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is function argument unpacking?

A
Let's say you have a function with the following signature/.
def print_vector(x,y,z):
    print(f'x:{x},y:{y},z:{z}')

if having a list with three elements, instead of passing each element separately, you can use list unpacking operator to simply do the following:
print_vector(*list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly