Feb-20-2022, 02:01 PM
Hello Forum,
a) I understand that everything that gets loaded in RAM (classes, variables, lists, modules, etc.) has a name. The object and the name of each object are recorded somewhere in RAM when the object is used. The name of each object is part of a naming structure which is like a phonebook. That phonebook is like a symbol table and is called namespace.
b) Thanks to namespaces, the Python interpreter can access variables, classes, et.c and avoid naming conflicts. Each namespace is essentially a different symbol table/name table. Functions have their own private namespace (local namespace), modules have their namespace (module namespace), the standard library has its own namespace (built-in namespace), and the running Python program has also its namespace (global namespace).
c) Each namespace has its own associated dictionary system (name:object mapping). Using the dot notation, we type <moduleName>.<moduleAttribute>. It looks like in some cases we need to specify the object's name to access its attributes but we don't in other cases if the objects' names are inside the built-in namespace or the global namespace...
d) Variables have a scope. Only variables have a scope or even other types of objects? For example, variables defined inside a function or class are only usable within and by that function or class and are local variables with a local scope. On the other hand, variables that can be used anywhere in the code are global. We run into issues when global and local variables have the same name...Creating global variables is discouraged...But how can we not create global variables if we write a Python script? We can certainly use modules and import functions, classes, etc. Do global scope variables/objects belong to a specific namespace or do they exist in all namespaces at the same time?
e) Is it correct to think that every namespace has its own scope and visibility? functions have a private, local namespace (all its internal variables cannot be seen outside of the function). But the function can see variables in the global namespace. Functions look inside their namespace to find variables that they are supposed to use. If the names of those variables are absent, the function looks in the global namespace. If nothing is there, the function looks in the built-in namespace.
f) Using import * is discouraged because it can create name conflicts. But it is convenient because we don't need to always write the module's name before its attributes. What is the issue with import * from a namespace point of view? What kind of namespace does import * create or not create?
Gasp! Thank for any brief correction.
bytecrunch
a) I understand that everything that gets loaded in RAM (classes, variables, lists, modules, etc.) has a name. The object and the name of each object are recorded somewhere in RAM when the object is used. The name of each object is part of a naming structure which is like a phonebook. That phonebook is like a symbol table and is called namespace.
b) Thanks to namespaces, the Python interpreter can access variables, classes, et.c and avoid naming conflicts. Each namespace is essentially a different symbol table/name table. Functions have their own private namespace (local namespace), modules have their namespace (module namespace), the standard library has its own namespace (built-in namespace), and the running Python program has also its namespace (global namespace).
c) Each namespace has its own associated dictionary system (name:object mapping). Using the dot notation, we type <moduleName>.<moduleAttribute>. It looks like in some cases we need to specify the object's name to access its attributes but we don't in other cases if the objects' names are inside the built-in namespace or the global namespace...
d) Variables have a scope. Only variables have a scope or even other types of objects? For example, variables defined inside a function or class are only usable within and by that function or class and are local variables with a local scope. On the other hand, variables that can be used anywhere in the code are global. We run into issues when global and local variables have the same name...Creating global variables is discouraged...But how can we not create global variables if we write a Python script? We can certainly use modules and import functions, classes, etc. Do global scope variables/objects belong to a specific namespace or do they exist in all namespaces at the same time?
e) Is it correct to think that every namespace has its own scope and visibility? functions have a private, local namespace (all its internal variables cannot be seen outside of the function). But the function can see variables in the global namespace. Functions look inside their namespace to find variables that they are supposed to use. If the names of those variables are absent, the function looks in the global namespace. If nothing is there, the function looks in the built-in namespace.
f) Using import * is discouraged because it can create name conflicts. But it is convenient because we don't need to always write the module's name before its attributes. What is the issue with import * from a namespace point of view? What kind of namespace does import * create or not create?
Gasp! Thank for any brief correction.
bytecrunch