Oct18_round Flashcards

1
Q

difference between set update and set union

A

update changes the set it is called on, i.e. s.update(s1) will change s by adding s1 to it. whereas union will create a new object containing both s and s1 without modifying either.

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

what are the 3 types of comprehension in python

A

list, dict and set

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

The default argument in a python function is evaluated once. Give an example.

A
def f(a, L=[]):
     L.append(a)
     return L
f(1) L = [1]
f(2) L = [1,2]
etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

if you have a list of argument L but the function f needs them unpacked, i.e. as induvidual elements, what is the syntax?

A

f(*L)

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

if you have a dictionary of arguments and a function needs them induvidually, what is the syntx

A

function(**dictionar_args)

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

what function shows all the attributes of an object (or all the names in the object’s namespace)

A

dir(object)

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

what is the difference between the global and nonlocal keyword when used before a variable name

A

global means “place this variable in the global scope and if it’s already there, use it.”
nonlocal means “place this variable in the nearest enclosing scope and if it’s not there flag an error”

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

if python finds “none” against the name of a module in sys.path, what does it do? When it does not find the path itself what does it do?

A

if it is mapped to none - modulenotfound exception is raised. Otherwise it cointinues searching.

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

what is the command to upload a python package to pypi so everone can use it?

A

python setup.py reigster sdist upload

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

what is the name and type of special variable that you define in __init__.py which specifies which modules are loaded into the namespace when the enclosing package is created.

A

__all__ and type is list

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

how do you create a tuple with a single element

A

tup = (“singleton”,) #the races are optional

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

what is the onlynamespace that remains from beginning to end in a python script?

A

The namespace containing the built in names. This is loaded when the python interpreter starts up.

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

what is the lifetime of a function namespace?

A

created when a function starts and destroyed when the function exits

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

when is the global namespace created?

A

when the module is read in

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

what are the different types of expressions?

A

list, set and dict

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

set and dict comprehensions both use curly bracer; what’s the difference in syntax?

A
set = {x for x innumbers}
dict = {x:x for x in numbers}
17
Q

what kind of a comprehension is this?

x for x in numbers

A

none, this creates a generator object

18
Q

what;s wrong with this

next(agenfunction), where agenfunction is a generator function? and how to solve the problem?

A

you need to use the return value from the generator function, i.e.
a = mygenfunction()
next(a)

19
Q

what is the difference between a single and double underscore?

A
single underscore  = attribute should be used by a class object, e.g. classname()._variable
double = variable shold only be used internal to the class and not by the clas object or any instance object of the class.