Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing classes/functions with same name as module

Say I use a python package with the following structure:

package/
   bar.py
   foo.py
   __init__.py

bar.py contains the class bar and foo.py contains the function foo. When I want to import the function/class do I have to write

from package.bar import bar
from package.foo import foo

or can I write

from package import bar
from package import foo

More generally asked: Can I always omit the class/function name, when I import a module with the same name as the class/function?

like image 981
Padix Key Avatar asked May 19 '17 07:05

Padix Key


People also ask

Can two modules have functions with the same name?

It works fine. Both the function calls would be executed and we get the results. We have to be careful if two modules has same function with different parameters while importing.

Can a module and class have the same name in Python?

There are times when a module and a class in it have the same name. Then, in order to access the class name using method 1 (see p. 1), you must prefix it with the module name. If you do not specify a module name, an exception will be thrown.

What happens if you define two functions in a module that have the same name in Python?

Python does not support function overloading. When we define multiple functions with the same name, the later one always overrides the prior and thus, in the namespace, there will always be a single entry against each function name.

How do I import all functions from a module?

The import statement You can use the functions inside a module by using a dot(.) operator along with the module name. First, let's see how to use the standard library modules. In the example below, math module is imported into the program so that you can use sqrt() function defined in it.


1 Answers

No, you can't omit the module or object name. There is no mechanism that'll implicitly do such imports.

From the Zen of Python:

Explicit is better than implicit.

Note that importing the module itself should always be a valid option too. If from package import bar imported the package.bar.bar object instead, then you'd have to go out of your way to get access to package.bar module itself.

Moreover, such implicit behaviour (auto-importing the object contained in a module rather than the module itself) leads to confusing inconsistencies.

  • What does import package.bar add to your namespace? Would referencing package.bar be the module or the contained object?
  • What should happen to code importing such a name, when you rename the contained object? Does from package import bar then give you the module instead? Some operations will still succeed, leading to weird, hard to debug errors, instead of a clear ImportError exception.

Generally speaking, Python modules rarely contain just one thing. Python is not Java, modules consist of closely related groups of objects, not just one class or function.

Now, there is an inherent namespace collision in packages; from package import foo can refer both to names set on the package module, or to a nested module name. Python will first look at the package namespace in that case.

This means you can make an explicit decision to provide the foo and bar objects at the package level, in package/__init__.py:

# in package/__init__.py
from .foo import foo
from .bar import bar

Now from package import foo and from package import bar will give you those objects, masking the nested modules.

The general mechanism of importing objects from submodules into the package namespace is a common method of composing your public API whilst still using internal modules to group your code logically. For example, the json.JSONDecodeError exception in the Python standard library is defined in the json.exceptions module, then imported into json/__init__.py. I generally would discourage masking submodules however; but foo and bar into a module with a different name.

like image 58
Martijn Pieters Avatar answered Oct 23 '22 05:10

Martijn Pieters