10

It seems that python do have namespaces. But All I can get is people telling me what namespace or scope is. So how do you define a namespace in python? All I need is the syntax (and with an example would be much better).

5
  • possible duplicate of stackoverflow.com/questions/3913217/… Commented Sep 28, 2016 at 9:52
  • 1
    @j4ck So which answer to that question has already answer my question? Commented Sep 28, 2016 at 9:54
  • 2
    The first one. Name spaces are nothing you can explicitly define. They're given by the scope you're coding in. Commented Sep 28, 2016 at 9:55
  • For example, if you define a variable within a function definition, that variable is within the scope and namespace of this function only, meaning other functions or parts of code cannot see it. Commented Sep 28, 2016 at 9:56
  • Maybe this might help to shed some more light ? Commented Sep 28, 2016 at 9:59

1 Answer 1

6

A "namespace" in Python is defined more by the layout of the code on disk than it is with any particular syntax. Given a directory structure of:

my_code/
    module_a/
        __init__.py
        a.py
        b.py
    module_b/
        __init__.py
        a.py
        b.py
    __init__.py
    main.py

and assuming each a.py and b.py file contains a function, fn(), the import syntax to resolve the namespace works like so (from main.py):

from module_a.a import fn  # fn() from module_a/a.py
from module_a.b import fn  # fn() from module_a/b.py
from module_b.a import fn  # fn() from module_b/a.py
from module_b.b import fn  # fn() from module_b/b.py

At this point, fn() is available within main.py and will call whichever implementation you imported.

It's also possible to use the from module import * syntax, but this is discouraged in favour of being more specific:

from module_a.a import *

Here, fn() is available within main.py, and also any other symbol defined in module_a/a.py.

If we want to have access to both module_a/a.py's fn() and also the fn() from module_b/b.py, we can do one of two things: either we use the from module import thing as something syntax:

from module_a.a import fn as module_a_fn
from module_b.b import fn as module_b_fn

and use them in our main.py as module_a_fn() and module_b_fn(), or we can just import the module and reference it directly in the code, so in main.py:

import module_a.a
import module_a.b

module_a.a.fn()  # call fn() from module_a/a.py
module_a_b.fn()  # call fn() from module_a/b.py

I hope that helps elucidate the usage a bit more.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer doesn't consider Classes, NamedTuples, or other things that define scope and are therefore namespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.