Reading-Notes

View the Project on GitHub MohammadAl-khatib/Reading-Notes

Game of Greed 2

Python Scope

Scope is the place in your code where you can access names and variables with their bare name, there are 2 main types of scopes:

  1. Global (G) scope: names and variables defined here are available every where in your code.
  2. Local (L) scope: here, they are only available within the scope.
  3. Enclosing (E) (or nonlocal) scope: like nested loops (inner can access outer names)
  4. Built-in scope (B): holds the built in functions and objects and loaded automatically, names will be searched in this scope lastly (L->E->G->B)

More comprehensive discussion can be found here

Keep in mind that name here may refer to variables, functions, modules, and classes, name pop into existence after assigning, definition, and importing, the place at which any previous activity takes place will be the scope of its name.

Python scopes are implemented as dictionaries, where every scope name is related to an object, you can access them on the higher level (globally) by importing sys and writes sys.__dict__.keys() for names of scopes.

You can access scopes by 2 ways:

A variable inside global scope can not be accessed locally except with global statement, when a local variable is created with the same name of a global variable; it will be treated as a new variable and it will not reference the global one.

Here are some tips regarding using scopes:

  1. Write self-contained functions that rely on local names rather than global ones.
  2. Try to use unique objects names, no matter what scope you’re in.
  3. Avoid global name modifications throughout your programs.
  4. Avoid cross-module name modifications.
  5. Use global names as constants that don’t change during your program’s execution.

Don’t be CONFUSED by BIG O notation anymore!

The modes of Big O can be defined as following:

  1. Constant
  2. Linear
  3. Logarithmic
  4. Quadratic
  5. Exponential
  6. Factorial

Big O diagram

watch video: Don’t be CONFUSED by BIG O notation anymore!