{
  "id": 1464423,
  "title": "Python Scope, First-Class Functions, *args,**kwargs & Mutable Default Arguments",
  "url": "https://urgent.news/2026/08/17/python-scope-first-class-functions-args-kwargs-mutable-default",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-17T10:15:22.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/deepika_pusala/python-scope-first-class-functions-argskwargs-mutable-default-arguments-1dbc"
  },
  "original_language": "en",
  "account": "Python provides several advanced concepts that may initially seem complex, but become easier to grasp once their underlying mechanism is understood. This guide explores: LEGB Scoping Rule, First-Class Functions, *args and **kwargs, Mutable Default Argument Pitfall, and Safe ways to use default arguments.\n\n1. LEGB Scoping Rule: Python follows a specific search order to locate variables, known as the LEGB rule. The acronym stands for Local, Enclosing, Global, and Built-in scopes. Python stops searching once it finds the variable in the first applicable scope.\n\n2. Local Scope: Variables defined inside a function are considered local variables. They can only be accessed within that function. An example:\ndef greet():\nname = \"Deepika\"\nprint(name)\ngreet()\n\nIn this case, \"name\" is local to the greet() function and cannot be accessed outside of it. Attempting to do so results in a NameError.\n\n3. Enclosing Scope: When a function is defined inside another function, it inherits the scope of the outer function. This is known as the enclosing scope. For example:\ndef outer():\nname = \"Deepika\"\ndef inner():\nprint(name)\ninner()\n\nouter()\n\nHere, \"name\" is in the enclosing scope of the inner function, not local to it. This concept is crucial for understanding closures.\n\n4. Global Scope: Variables defined outside any functions typically reside in the global scope. They can be accessed throughout the module. For example:\nname = \"Deepika\"\ndef greet():\nprint(name)\ngreet()\n\nThis will output \"Deepika\" because \"name\" is found in the global scope, not in the local scope of greet().\n\n5. Built-in Scope: Python provides built-in functions like print(), len(), sum(), max(), min(), and type(). These are part of the built-in scope, and can be accessed directly without defining them within the script. For example:\nnumbers = [10, 20, 30]\nprint(len(numbers))\n\nThe \"len\" function is found in the built-in scope.\n\n6. Mutable Default Argument Pitfall: Default argument values in function definitions are evaluated once when the function is defined, not each time the function is called. This can lead to unexpected behavior if mutable types like lists or dictionaries are used as default arguments. For example:\ndef append_item(item, my_list=[]):\nmy_list.append(item)\nreturn my_list\n\nprint(append_item(\"a\")) # Output: ['a']\nprint(append_item(\"b\")) # Output: ['a', 'b']\n\nThe same list object gets modified in each function call, leading to unexpected results.\n\n7. Safe ways to use default arguments: To avoid the mutable default argument pitfall, you can use None as a default value and create a new list inside the function if the argument is None. This ensures a fresh list is created for each function call.\n\ndef append_item(item, my_list=None):\nif my_list is None:\nmy_list = []\nmy_list.append(item)\nreturn my_list\n\nThis way, each function call gets its own list, avoiding the mutable default argument pitfall.",
  "summary": "Python has several concepts that look difficult at first, but they become simple once we understand what Python is actually doing. This guide covers: LEGB Scoping Rule Local, Enclosing, Global and Built-in scopes nonlocal First-Class Functions *args **kwargs Packing and Unpacking Mutable Default Argument Pitfall Safe ways to use default arguments 1. LEGB Scoping Rule What is Scope? Scope means…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}