Fichas
python function characteristics
Fichas del conjunto (29)
- 1what can functions be 'generalized' as ?as "callable" (ie via __callable__)
- 2how can you make callable objects from instances?implement __call__()
method - 3what are 2 flavors of arguments to a function?positional and keyword
- 4what is the difference between keyword argument and positional argument?keywords use 'keyword and value' as in arg2=1.618
- 5can keyword arguments come before positional arguments?no, keyword arguments come after positional
func(arg1, arg3=1.4) - 6what about ordering of keyword args?keyword args can be in ANY order provided they all follow any positional arguments
- 7how many times is default value evaluated if a function is called 3 times?it is only evaluted 1 time, first time.
- 8how can you make a class runnable?you can make a class callable by implementing
__call__(self,arg):
function - 9how would you represent : if condition: ` result=true_value ` else : ` result= false_value ` using a conditional expression?result=true_val if condition else false_val
- 10what useful function can sort a string of fname and lname, based on lastnamesortable(iterable, key=None, reverse=False) --> new sorted list
- 11example of sorting list using sorted function: scientists=['mar cur', 'al eins', 'niels bor']sorted( scientists,
key=lambda name: name.split() [-1]) - 12how would you extract last name from x="nikola tesla" using lamdas?last_name=lambda ln: ln.split()[-1]
then callit : last_name(x) - 13difference between function and lambdamethod call is a STATEMENT that defines a function and bind it to a name.
lambda is an EXPRESSION that evaluates to a function - 14can lambdas have name?no, lambdas are anonymous
- 15how are arguments supplied to lambda?argument list is separated by commas, terminated by colon
- 16how do you represent empthy () arguments to a lambda ?zero arguments=>
lambda: - 17what is the block of a lambda?the block of a lambda is a 'single expression'
- 18how do represent the return value for a lambda?the return value for a lambda is given by the BODY of the EXPRESSION. return statement is not allowed.
- 19can lambdas use docstrings?no
- 20how can you test lambda?awkward or impossible, keep it simple
- 21how can you tell if object is callable?use callable(func_name)
returns T / F - 22how can you handle various sizes of arguments passed in?use func(*args)
- 23how to determing number of arguments passed in to *argsi=iter(args)
v=next(i)
or better:
func(arg1, *args)
len=arg1
for item in args - 24how would you construct func parameters for following call: func('headername', src1="data1", src2="data2")func(hname, **kwargs)
==> gives you one string and one dictionary
hname=headername
kwargs={src1:"data1", src2:"data2"} - 25is this allowed? func(*args, **kwargs)no
- 26is this allowed? func(*args, **kwargs, kwargfinal)no
- 27how do you unpack a sequence? such as t=(11,12,13,14)use *t , such as in
func(*t) - 28what does : def print_args(arg1,arg2,*args): print(arg1) print(arg2) print(arg3) t=(11,12,13,14) print_args(*t)11
12
(13,14) - 29—
Información
- Descripción
- (beyond basics python flashcards) python Fichas sobre python function characteristics, creado por Andrew Sperazza el 09/01/2017.
- Creado por
- Andrew Sperazza
- Visitas
- 10
- Etiquetas
- Valoración