- Object Reference
- Value has type, Variable do not has type
- No Declaration Needed
- Define by assigning:
<Var> = <Value>
- Life Cycle: Exist when needed, disapear when not needed
- Inside where defined: Module, Function, Class, Object
- Access
- Access Global Variable in Local Scope:
global_variable = dummy_1
def fuction_1():
global global_variable
# Do something with global_variable
def fuction_2():
global_variable = dummy_2
# Wrong way: override global variable with same-name local variable
- Return a copy of current object:
object.copy()
- Annotation: A expression associated with the variable
- Expression is evaluated when variable defined
<Variable>[: <Annotation>][ = <Value>]
- Used for type hint and check
- Built-in Container Types cannot be used in Type Annotations to indicate its element's type
- Use abstract base classes in
typing
module to support indicating element type of container type
- Sequence:
<ContainerType>[<ElementType>]
- Dictionary:
<Variable>: Dict[<KeyType>, <ValueType>]
- Generic Container Type is defined with capitalization
- E.g.:
list (built-in) -> typing.List (Capitalized)
from typing import <ContainerType>
var: <ContainerType>[<ElementType>] # VALID