Scope Flashcards

1
Q

what is scope

A

scope dictates whether or not a variable/function is available in certain parts of the program
- scope rules apply to module/variable/function identifiers

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

Global scope

A
  • usable anywhere in the program

- use is discouraged

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

problems with global scope

A
  • many issues in large programs due to # of functions and modules which could change them
  • if statements could change global variables accidentally
  • programmer error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

global variable restrictions

A
  1. can get value of global variable inside a function

2. cannot give new value to a global variable inside a function unlesss using global keyword

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

Constant identifiers

A
  • for example PI, a value that will not change
  • typically styled with all caps
  • python doesn’t have constant, so instead create global variable with all caps
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Important GV notes

A
  • python has a list of special variables

- not really different from global scope but handled by python instead of programmer

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

local scope

A
  • variable in a function, therefore its scope is limited to the function
  • can have same name as global variable but are different entitites
  • function parameters all have local scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ID()

A

use to test if variables are aliases

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

Important list/dict scope

A
  1. if necessary to make a list/dict inside a function don’t reuse name
  2. change elements as needed but keep in mind when using list/dict as a parameter it is an alias of the argument so change content of parameter is to change the argument
How well did you know this?
1
Not at all
2
3
4
5
Perfectly