4. User defined Function Flashcards

1
Q

basic structure

A
def myname_of_function(arg1,arg2):
    return arg1 + arg2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

default an argument

A

def my_function(name = ‘Default’):

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

indefinite arguments in a func

A
def myfunc(*args):
    print(args)

args return back a tuple

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

key word argument

A
def myfunc(**kwargs):
    if 'fruit' in kwargs:

myfunc(fruit = ‘apple’, veggie = ‘lettuce’)

kwargs return back a dictionary

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

map function

A

map( function ***no parenthesis, iterables)

map will apply the function into each iterables

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

filter function

A
filter down an iterables.
function has to return boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

lambda expressions

A

a function only use one time and will not be called again, without the def or a function name

lambda num:num**2

usually use together with map / filter

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

LEGB rule

A

local: within in function
-> Enclosing function local ->
global -> built-in

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

global assignment in a function

A
func():
     global x
    x=50   --> affect globally

try to avoid using global

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

passing function in function

A

function is first class object

def a(x):
    return "a(%s)" % (x,)
def b(f,x):
    return f(x)

print b(a,10)

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

what is a Generator?

A

Generator can send back a value and then later resume to pick up where it left off.
thus generate a sequence of values over time instead of storing all of the values in memory
range is a type of generator

it’s all about memory efficient in a for loop.
to see all the values, cast it to a list

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

how to operate generator

A

next and iter

g = simple_gen()
print(next(g))
print(next(g))

iter turn something iterable into a generator

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

generator comprehension

A

mylist = [1,2,3,4,5]

gencomp = (x for x in mylist if x > 3)

g = gencomp

note that a generator object is different from a generator function

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

Decorator

A
@new_decor
def func_needs_decorator():
    print("I want to be decorated!!")

this pass the func_needs_decorator() into a wrapper function “new_decor”. the @ line act as an on/off

function can return function, after assignment it can be called

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

passing a function as an argument

A
def other(wahtevername):
    print("something")
    print(whatevername())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly