Chapter 9 (functions) Flashcards

1
Q

what can be used to check for global and local variables?

A

globals()
locals()
- These variables act as dictionaries that store the key (variable name) with its value (assigned data to the variable)

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

What is variable scope?

A

How accessible a variable is to sections of code:
- if a variable is local, it can only be accessed within the function, if it is global it can be accessed anywhere in the code

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

What are the 3 types of scope?

A

Built-in scope:
- can be accessed anywhere with no restrictions, they are the built-in names of python: str(), int(), etc.
Global scope:
- variables that have been declared outside of functions
Local scope:
- only accessible in a function

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

In what order are variables checked in a function?

A
  • Local
  • Global
  • Built-in
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is namespace?

A

maps (like a dictionary) of names of objects in a given scope

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

What is scope resolution?

A

The process of searching namespace for a name,
- looking for a variable in the dictionary of names

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

How does python interpreter restrict variable scope?

A

assigning variables to a namespace

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

Can the same name be in multiple namespaces?

A

Yes

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

Will there be an error if I define a list as list() since it is already a built-in name?

A

No because it is going to be defined as a global or local variable which are read before the built-in names

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

Can immutable objects be edited inside a function?

A

no, the object will not be edited, instead a new object will be made in the local scope of the function with the values assigned to it instead of editing the global variable

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

Can mutable objects be edited inside a function?

A

Yes they can, for example a list or dictionary, however, only edited, not replaced.
- index overwriting is fine, but straight up changing the whole list all at once will not impact the list

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

what would be the output of:
def modify(list):
‘’’ docstring ‘’’
list[1] = 99
my_list = [1,2,3]
modify(my_list[:])
print(my_list)

A

[1,2,3] because the argument is actually a copy of my_list not the real list

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

When preassigning values to parameters, what must be true?

A

All parameters that have preassigned values must be in the correct order

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

Can you put the arguments in any order when calling a function?

A

Sometimes, only when the parameter is specified = a value

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

What is an arbitrary parameter?

A

When the number of parameters cannot be predicted, so the function excepts more than normal all stored in an arbitrary parameter

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

What are some examples of arbitrary parameters?

A

*args (arguments)
**kwargs (key word arguments, stores a dictionary)

16
Q

What are some examples of arbitrary parameters?

A

*args (arguments)
**kwargs (key word arguments, stores a dictionary)

17
Q

Can a function return more than one object?

A

no, however a tuple of multiple values can be returned

18
Q

What does the follwing line do?
def f():
‘’’ docstring ‘’’
return val1,val2
val3, val4 = f()

A

val3 = val1
val4 = val2
val1 and val2 where returned in a tuple

19
Q

MUST INCLUDE DOC STRING OR -2 POINTS

A
20
Q

what does help(function) do?

A

Instead of running the function, it outputs the doc string

21
Q

How do you define a function?

A

def function():
‘’’ docstring ‘’’
return value

22
Q

How do you pass a create a function with a fixed value incase the variable is not passed?

A

def function(a, b=25):

23
Q

How do you selectively run code depending on if the file is being run as a main program or an imported module?

A

if __name__ = ‘__main__’:
—-code for regular file
code if file was imported as a module

24
Q

When returning a tuple from a function, how do you print an index from the tuple?

A

print(function(pass)[index])

25
Q

When setting a default value in a function, what must be true in terms of order?

A

The default values must be declared last

26
Q

What happens if there are two return statements in a function?

A

The first return statement that is encountered will end the function and return to the main code