Python - shivamvats/notes GitHub Wiki

  1. Closure: A nested function ( a function defined inside another) is a closure if it references/uses a variable from the outer scope. In such a case, the closure saves references to those variables. Note, this means that if the referenced variable changes, this is reflected in the closure.
  2. Scoping Rules: Python follows LEGB: Local -> Enclosing -> Global -> Built-in scoping. The smallest scope is a function. List comprehensions and generator expressions are internally implemented as functions, so they too have a scope. For details : LEGB and list-comprehensions_
  3. == vs is: The difference is basically equality vs identity. The is operator checks if LHS is the same object as the RHS (i.e. same memory address). == defaults to is, but is usually overloaded to compare values. For examples, a, b = [1, 2], [1, 2]. a == b but a is not b.
  4. None: None is a singleton object. Hence, is operator works just fine.

Tools

  1. mypy : Optional static type checking in Python.
⚠️ **GitHub.com Fallback** ⚠️