Things you should know about Python:

๐Ÿ”น ๐๐š๐ฆ๐ž๐ฌ๐ฉ๐š๐œ๐ž:

A namespace is a mapping from names to objects. Simply put, it's like a system that assigns a unique name to every object (such as variables, functions, or classes) in Python.

๐Ÿ”น ๐’๐œ๐จ๐ฉ๐ž:
A scope is a textual region of a Python program where a namespace is directly accessible.

Or in Simply, the scope defines the region of code where a name is valid. It determines from which part of the code you can access a particular Python object (like a variable or function).

๐Ÿ”น ๐“๐ฒ๐ฉ๐ž๐ฌ ๐จ๐Ÿ ๐’๐œ๐จ๐ฉ๐ž:
Local Scope: Contains variables & fxn defined within the current function or block of code.
Enclosing Fxn Scopes: Include non-local names from outer functions in nested functions.
Global Scope: Encompasses global names defined at the top level of the current module.
Built-in Scope: Holds Python’s default names (like print() or len()), always available.

๐Ÿ”น ๐๐จ ๐‘๐ž๐ฅ๐š๐ญ๐ข๐จ๐ง ๐/w ๐๐š๐ฆ๐ž๐ฌ ๐ข๐ง ๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐๐š๐ฆ๐ž๐ฌ๐ฉ๐š๐œ๐ž๐ฌ:
Names in different namespaces have no direct connection. For instance, two different modules can both define a function called maximize, and Python won’t get confused. To use such fxns, users must prefix them with the module name to avoid ambiguity.

๐Ÿ”น ๐€๐ญ๐ญ๐ซ๐ข๐›๐ฎ๐ญ๐ž๐ฌ ๐จ๐Ÿ ๐š๐ง ๐Ž๐›๐ฃ๐ž๐œ๐ญ ๐š๐ฌ ๐š ๐๐š๐ฆ๐ž๐ฌ๐ฉ๐š๐œ๐ž:
An object’s methods and properties also form a namespace. It’s like each object has its own little container for its features.

๐Ÿ”น ๐–๐ก๐ž๐ง ๐€๐ซ๐ž ๐๐š๐ฆ๐ž๐ฌ๐ฉ๐š๐œ๐ž๐ฌ ๐‚๐ซ๐ž๐š๐ญ๐ž๐?:
Namespaces are created at different moments & have different lifetimes.

The built-in namespace is created when the Python interpreter starts up & never deleted.
The global namespace for a module is created when the module definition is read in. Normally, module namespaces last until the interpreter quits.

The local namespace for a function is created when the function is called, & deleted when the function returns or raises an exception that is not handled within the function.

๐Ÿ”น ๐Œ๐จ๐๐ฎ๐ฅ๐ž ๐๐š๐ฆ๐ž๐ฌ๐ฉ๐š๐œ๐ž:
When you define a function in a module (a Python file), its global scope is that module’s namespace. Imagine the module as a room, & all the names you define inside it are part of that room’s namespace. No matter where you call the function from (even if you use an alias), Python looks inside that module for the function.

๐Ÿ”น ๐ƒ๐ฒ๐ง๐š๐ฆ๐ข๐œ ๐๐š๐ฆ๐ž ๐’๐ž๐š๐ซ๐œ๐ก:
When you use a name (like calling a function), Python searches for it dynamically at run time. It’s like asking someone in the house to find a specific object for you. But here’s the catch: Python is evolving toward static name resolution (like figuring it out during “compile” time). So, don’t rely too much on dynamic search!

๐Ÿ”น ๐ฆ๐š๐ข๐ง ๐Œ๐จ๐๐ฎ๐ฅ๐ž:
When you run Python code interactively or from a script file, it’s considered part of a module called __main__ which has its global namespace.

Post a Comment

Previous Post Next Post