05-Python intermediate

Deepcopy and shallow copy, Value equality vs identity

πŸ“… Jul 5, 2024
# Topic covered
* Deepcopy and Shallow copy
* Value equality vs identity

Deepcopy and Shallow copy

  • In case of shallow copy, a reference of object is copied in other object.
    • It means that any changes made to the copied object do reflect in the original object
    • External object id changes but Internal object id remains same
  • In case of deep copy, a copy of object is copied in other object.
    • It means that any changes made to the copied object do not reflect in the original object
    • External and internal object ids changes
Read More β†’

10-Python Basic

Scopes and Name Resolution

πŸ“… Jul 6, 2024

Scopes and Name Resolution

Local scope - inside a function

Enclosing scope - from outer function if nested

Global - Top leve script

def serve_chai():
    chai_type = "Masala" # local scope
    print(f"Inside function {chai_type}")
chai_type = "Lemon"
serve_chai()
print(f"Outside function: {chai_type}")


def chai_counter():
    chai_order = "lemon" # Enclosing scope
    def print_order():
        chai_order = "Ginger"
        print("Inner:", chai_order)
    print_order()
    print("Outer: ", chai_order)
chai_order = "Tulsi" # Global
chai_counter()
print("Global :", chai_order)

NonLocal - The nonlocal keyword is used to modify a variable in an enclosing but non-global scope, which is specifically relevant for nested functions.

Read More β†’