What many other languages do
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)
get_xyz(argument)
For example, if I want the argument to be the number "5", the I can do:
get_xyz(5)
get_xyz(5)
What we have done in Python
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)
get_xyz.get_xyz(argument)
in Python, which looked very redundant. One programmer changed it to something like:
go_db.get_xyz(argument)
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
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)
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?
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?