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
2
Q
Global scope
A
- usable anywhere in the program
- use is discouraged
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
4
Q
global variable restrictions
A
- 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
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
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
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
8
Q
ID()
A
use to test if variables are aliases
9
Q
Important list/dict scope
A
- if necessary to make a list/dict inside a function don’t reuse name
- 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