What many other languages do
In many languages, I can have a function defined in a file called get_xyz.m (for example) and then other functions or scripts can use that function with a syntax like:
get_xyz(argument)
For example, if I want the argument to be the number "5", the I can do:
get_xyz(5)
What we have done in Python
Python doesn't allow "functions as files" unless we add lines like from get_xyz import get_xyz to the beginning, which seems like a waste of space and can make the file quite long if there's a lot of them to import. Instead it we can have a "module" file which can contain functions, but they can't be called in such a simple way like in other languages. The reason this question arose for me while working on a project with other team members, is because we had something like:
get_xyz.get_xyz(argument)
in Python, which looked very redundant. One programmer changed it to something like:
go_db.get_xyz(argument)
which I found to be a big more confusing and it seemed "forced", as if we didn't really want to come up with a second name, but it was done in order to make the "module" and "function" have different names.
What others have done in Python
Others have also run into this issue in their Python programs, and one example can be seen here for a Python program that currently has 898 stars and 475 forks on GitHub:
fci.FCI(argument)
Here the "module" name and the function name are distinguished by using lower case and capital letters.
Why can't we just call functions like in other languages?
I debated for a while with two hard-core Python enthusiasts about this, and the only answer that I eventually got at the very end was:
"This solves the issue of namespaces, so that no two things have the same name."
Is that the only reason why Python was designed this way?
from get_xyz import get_xyz, e.g.from timeit import timeitorfrom decimal import Decimal, when a module has just one "main" export. This still obviously duplicates the name, but the import statement is where that duplication belongs, not the call-site. So I dispute the premise ─ Python does allow you to call the function likeget_xyz(5), it's just that the module is not the function. Perhaps the question should instead be e.g. why Python doesn't have a syntax for this kind of import, but really it's because it doesn't have a syntax for default exports. $\endgroup$export defaulta function from a module, such that when you import that module it binds the function to the imported name, rather than an object containing the module's exported names. $\endgroup$