When the main program calls f(), Python creates a new namespace for f(). On line 15, the main program calls f().Lines 4 to 7 define g(), the enclosed function.Lines 1 to 12 define f(), the enclosing function.In this example, function g() is defined within the body of f(). You can also define one function inside another: That namespace is local to the function and remains in existence until the function terminates.įunctions don’t exist independently from one another only at the level of the main program. The Local and Enclosing NamespacesĪs you learned in the previous tutorial on functions, the interpreter creates a new namespace whenever a function executes. For the moment, when you see the term global namespace, think of the one belonging to the main program. You’ll explore modules in more detail in a future tutorial in this series. Python Modules and Packages-An Introduction. For further reading on main functions and modules in Python, see these resources: The interpreter also creates a global namespace for any module that your program loads with the import statement. Strictly speaking, this may not be the only global namespace that exists. Python creates the global namespace when the main program body starts, and it remains in existence until the interpreter terminates. The global namespace contains any names defined at the level of the main program. This namespace remains in existence until the interpreter terminates. The Python interpreter creates the built-in namespace when it starts up. You’ll see some objects here that you may recognize from previous tutorials-for example, the StopIteration exception, built-in functions like max() and len(), and object types like int and str. Typically, many namespaces will exist at any given time. As Python executes a program, it creates namespaces as necessary and deletes them when they’re no longer needed. In a Python program, there are four types of namespaces: They’re honking great, and Python uses them extensively. Namespaces are one honking great idea-let’s do more of those!Īs Tim Peters suggests, namespaces aren’t just great. Each key-value pair maps a name to its corresponding object. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves. Namespaces in PythonĪ namespace is a collection of currently defined symbolic names along with information about the object that each name references. How variable scope determines symbolic name visibilityįree Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.How Python organizes symbolic names and objects in namespaces.How does Python keep track of all these names so that they don’t interfere with one another? In a program of any complexity, you’ll create hundreds or thousands of such names, each pointing to a specific object. The statement x = 'foo' creates a symbolic name x that refers to the string object 'foo'. Objects are everywhere! Virtually everything that your Python program creates or acts on is an object.Īn assignment statement creates a symbolic name that you can use to reference an object. The previous tutorials in this series have emphasized the importance of objects in Python. This tutorial covers Python namespaces, the structures used to organize the symbolic names assigned to objects in a Python program. Watch it together with the written tutorial to deepen your understanding: Navigating Namespaces and Scope in Python Watch Now This tutorial has a related video course created by the Real Python team.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |