Definition
A namespace is simply a dictionary of all currently defined names, and their values. Key, value pairs. The interesting part, is which symbolic name is available where and how to organise your files and methods to utilise the namespace concept as intended.
In a complex python program, we have hundreds, thousands of these names. To avoid interference and to be able to manage these symbolic names, we use the concept of namespaces. As mentioned above, the namespace is a dictionary of available key, value pairs.
Types of namespaces
There are 4 types of namespaces:
- Built-In
- Global
- Enclosing
- Local
No need to go into details here, these are basic coding concepts.
We will just write down some best practices.
Best practices
global keyword
Do not use the {python} global
keyword. It allows to modify variables inside of a function (local namespace), and make those changes on a different namespace level (global).
Mutables vs Immutable objects
How to modify global types inside of local namespaces: Mutable types
If you wish to modify objects inside of functions and make those changes last, most of the time there is no need to return that object. Just use mutable types (python) instead of immutable types (python)
# option 1 is better
def modify_mutable_option1(dummyClass):
dummyClass.counter += 1
dummyClass1 = DummyClass()
modify_mutable_option1(dummyClass1)
# option 2
def modify_mutable_option2(dummyClass):
dummyClass.counter += 1
return dummyClass
dummyClass2 = DummyClass()
dummyClass2 = modify_mutable_option2(dummyClass2)
Use immutable objects for fixed data
It prevents bugs and makes code safer.
Multithreading
If you're using multithreading, immutables have the advantage of being threadsafe.
Example:
Not thread safe:
{python}config = {"setting1": True, "setting2": 10} # mutable object
Thread safe:
{python} config = (true, 10) # immutable object
Threads can read it safely without worrying about one thread changing the object while another is reading it.
Issues arising in data science:
Relationship with packages
Namespaces is one of the reasons we use packages. The above issue (being unable to import from the parent folder) can also arise if you have multiple entrypoints to an app. This can happen if you use a testing framework, or a webserver with multiple entrypoints.
The solution for this are "editable installs": {shell}python -m pip install -e path/to/SomeProject
- Create a pip package from your project
- Install it in editable mode
- Import from anywhere inside of the project as you please
See Python packaging