In other languages, I know, you can create a single namespace, and spread it across different files.

And in the python did not hear about such a possibility. Maybe I missed something, and in fact there is such an opportunity? If not, for what reason? I would like to know.

So that there is no dispute over the need for this, I will give an example of a quote from the book "Algorithms and Data Structures":

A namespace is a higher-level abstraction, since, in general, one namespace can combine objects defined in different files, on the other hand, objects from multiple namespaces can be defined in one file. Thus, the concepts of physical and logical modularity are orthogonal.

  • It is not necessary, because in large projects it will be easy to get confused. But if you really want, then you can probably through exec (but still not needed) - andreymal

1 answer 1

Yes you can, using packages.

Example:

package / __ init__.py

 from .one import func from .two import func2 

package / one.py

 def func(): pass 

package / two.py

 def func2(): pass 

Using:

 import package package.func() package.func2() 
  • one
    I do not understand something? It seems to me that this is not exactly what I am talking about, since, after all, each file localizes in itself a separate namespace. - Mr Fix
  • @Mrfix one namespace: package . How functions are used from the outside and how they are implemented are two different things. Look at asyncio/__init__.py . - jfs
  • it's just outside. How about using a function from two directly in one? Have to import? Why, they are in "one namespace" - vp_arth
  • @vp_arth, depending on what problem the author of the question is trying to solve. If “to be” in the same way as in some other language does not work, all languages ​​have their own nuances. Regarding the use of the one functions from the two module - yes, you will have to import. No, the modules for each other are not in the same namespace. But why should the external observer (package user) have to worry about the implementation details? - insolor
  • one
    @jfs I was helped by the answer that there is no such possibility. But I can not mark it, as it is in the comments. - Mr Fix