<h1>What many other languages do</h1>
<p>In many languages, I can have a function defined in a file called <code>get_xyz.m</code> (for example) and then other functions or scripts can use that function with a syntax like:</p>
<pre><code>get_xyz(argument)
</code></pre>
<p>For example, if I want the argument to be the number &quot;5&quot;, the I can do:</p>
<pre><code>get_xyz(5)
</code></pre>
<h1>What we have done in Python</h1>
<p>Python doesn't allow &quot;functions as files&quot; unless we add lines like <code>from get_xyz import get_xyz</code> 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 &quot;<em><strong>module</strong></em>&quot; 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:</p>
<pre><code>get_xyz.get_xyz(argument) 
</code></pre>
<p>in Python, which looked very redundant. One programmer changed it to something like:</p>
<pre><code>go_db.get_xyz(argument) 
</code></pre>
<p>which I found to be a big more confusing and it seemed &quot;forced&quot;, as if we didn't really want to come up with a second name, but it was done in order to make the &quot;module&quot; and &quot;function&quot; have different names.</p>
<h1>What others have done in Python</h1>
<p>Others have also run into this issue in their Python programs, and one example can be seen <a href="https://github.com/pyscf/pyscf/blob/master/examples/fci/00-simple_fci.py#L29" rel="nofollow noreferrer">here</a> for a Python program that <a href="https://pyscf.org/" rel="nofollow noreferrer">currently has 898 stars and 475 forks on GitHub</a>:</p>
<pre><code>fci.FCI(argument)
</code></pre>
<p>Here the &quot;module&quot; name and the function name are distinguished by using lower case and capital letters.</p>
<h1>Why can't we just call functions like in other languages?</h1>
<p>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:</p>
<blockquote>
<p>&quot;This solves the issue of namespaces, so that no two things have the same name.&quot;</p>
</blockquote>
<p>Is that the only reason why Python was designed this way?</p>

<language-design><language-comparison><python><matlab-octave><functions>
Why does Python not allow us to call functions like other languages do?
CC BY-SA 4.0
2023-05-16T21:53:13.287Z
Typically you would write `from get_xyz import get_xyz`, e.g. `from timeit import timeit` or `from 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 like `get_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*.
kaya3
CC BY-SA 4.0
2023-05-16T22:01:21.747Z
I said this in my answer as well, but can you name another language where files are functions? I've only ever seen this pattern in MATLAB and I've always found it, to be frank, incredibly silly (more so than forcing files to be classes).
Silvio Mayolo
CC BY-SA 4.0
2023-05-16T22:10:09.037Z
@SilvioMayolo There's **tonnes** of examples. Other than MATLAB/Octave, there's also Fortran, Julia and probably most programs used in scientific computing, which is my area.
Nike Dattani
CC BY-SA 4.0
2023-05-16T22:49:37.490Z
The example I'd give is in Javascript, you can `export default` a 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.
kaya3
CC BY-SA 4.0
2023-05-16T22:51:30.580Z
@kaya3 I edited my question in response to your comment, but SE only allows us to "ping" one user at a time in comments, so I only replied to Silvio's comment the first time :)
Nike Dattani
CC BY-SA 4.0
2023-05-16T22:54:45.067Z
I see your edit and I do not think it addresses the problem with your question. `from get_xyz import get_xyz` does not make your code substantially longer than `import get_xyz`, which you already must be doing; it's the same number of lines, just one line is a bit longer, and it's not a particularly long line regardless. Even so, the question should be "why doesn't Python have a shorthand syntax for importing a single-function module?", given that the premise "why doesn't Python let me call the function this way?" is objectively false.
kaya3
CC BY-SA 4.0
2023-05-16T23:03:36.657Z
@kaya3 We don't have to run `import get_xyz` every time, if we already have the whole package loaded/installed in a different way. Also, the title of my question is not "why doesn't Python let me call the function this way?", it's longer than that, and when you include the part that you removed, it's no longer "false". Another title could be "Why doesn't Python let me do this without adding a `from get_xyz import get_xyz` line?" but we don't need to be so pedantic.
Nike Dattani
CC BY-SA 4.0
2023-05-16T23:20:22.443Z
If there is a "whole package" then the module is not just a single function, so the premise is even more flawed. What would it mean to write `get_xyz(5)` if `get_xyz` is a module which exports *multiple* functions? As I said, the premise is objectively false, because Python **does** allow you to call a function like `get_xyz(5)` **if** the name `get_xyz` is bound to the function rather than the module that declares it. The same is true in any other language; you can write `get_xyz(5)` to call a function **if** the name `get_xyz` refers to that function.
kaya3
CC BY-SA 4.0
2023-05-16T23:33:08.300Z
@kaya3 Does it not depend on how the `__init__.py` is structured? Also, can you please stop being so aggressive by repeatedly saying that everything is "objectively false" and "even more flawed", etc.? The premise of the title "Why does Python not allow us to call functions like other languages do?" is true in my opinion because it can't be done like it can in MATLAB/Octave, Fortran, etc., without adding something like `from get_xyz import get_xyz`, so it **cannot** do it like we can in those languages.
Nike Dattani
CC BY-SA 4.0
2023-05-16T23:45:06.683Z
"It can't be done without X" directly contradicts "it can't be done" ─ it can be done, with X. Your issue is that you think X is cumbersome, because it requires writing the name `get_xyz` twice in the import statement instead of once. But Python is not disallowing you from doing this. By framing your question in this way you make it impossible to answer without disputing the premise. You can write `get_xyz(5)` to call a function if `get_xyz` is the name of a function, you cannot if it is the name of a module instead. But why insist on having it be a module, if you want it to be a function?
kaya3
CC BY-SA 4.0
2023-05-16T23:52:42.543Z
@kaya3 I think that when you re-read your comments tomorrow, you might agree that you are splitting hairs. Again, I haven't said "It can't be done without X". I said "It can't be done the same way as it can in other languages", which I believe is true. I haven't made it "impossible" to answer without disputing the premise, because two answers have already been written, and if they dispute the premise, that's okay. Users are allowed to write answers that dispute the premise :)
Nike Dattani
CC BY-SA 4.0
2023-05-17T00:24:23.867Z
@NikeDattani I've re-read both of our comments several times, and not changed my assessment. It's like if you asked "how come ketchup doesn't come out of the ketchup bottle, like in other restaurants?" when the bottle in your hand is mustard. OK, you took the bottle from where you think the ketchup *should* be. But it's not a ketchup bottle, and if you *had* taken the ketchup, you'd see it works the same as other restaurants' ketchup bottles. What you really want to know is why the bottles are in different places, but you're focusing on your ketchupless plate instead of which bottle you took.
kaya3
CC BY-SA 4.0
2023-05-17T00:37:53.850Z
I totally disagree with your ketchup analogy. There's a reason why I said "tomorrow" in my last comment. You're not going to see anything new here within a 30 minute time frame, and neither am I.
Nike Dattani
<p>There are a few reasons:</p>
<ol>
<li>As you said, so no two namespaces have the same name.</li>
<li>Readability and clarity. The <code>module.function()</code> syntax provides clear information about where the function is defined making it more readable and clear, especially in big projects.</li>
<li>Modularity and Encapsulation. This helps organize code and improves its maintainability.</li>
<li>Explicit importing. It is a documentation method to show what's in the code and prevents function name collision between different modules.</li>
<li>Consistency and easy to use. It is also how you would go about getting attributes or object methods, making it more intuitive and easy to learn.</li>
</ol>

CC BY-SA 4.0
2023-05-16T21:57:00.513Z
2024-07-16T20:00:51.937Z
2024-07-16T20:00:51.937Z
Starship
CC BY-SA 4.0
2023-05-16T22:19:39.727Z
**(2)** I find `module.function()` unnecessarily verbose, rather than "more readable and clear" than `function()`. **(3)** How does it help here? **(4)** Is this not the same as (1)? **(5)** How is it more "intuitive" and "easy to learn" than allowing us to do function()? If you still think it's more intuitive and easy to learn, by how much? I picked up MATLAB way faster than I picked up Python. Within a week I could do more in MATLAB than I can do in Python after a few years (albeit not doing full-time Python programming during these years).
Nike Dattani
<h1>This pattern is not as common as you think</h1>
<p>It looks like you're referencing MATLAB there. MATLAB is the only language I know that forces a file to be in one-to-one correspondence with the language's notion of a &quot;function&quot;. In my experience, when a language ties an existing construct to the source files making up your program, it just limits what you can do.</p>
<ul>
<li>MATLAB forces files to be public functions, and that forces me to split up several small but public functions into extremely tiny files.</li>
<li>Java forces files to be public classes. This results in several awkward patterns where I have so-called &quot;classes&quot; containing only static methods.</li>
</ul>
<p>The languages that have the most success leveraging the file system do so by creating a new construct in the language itself specifically <em>for</em> the notion of a file. Python, and many languages, calls these construct a &quot;module&quot;. A module is not an existing language feature. It's a new thing that can essentially be defined as &quot;the reification of a file within Python&quot;. As such, its needs are exactly built to suit files and are not shoehorning the filesystem into an existing model of &quot;functions&quot; or &quot;classes&quot;.</p>
<p>It's telling that the two most popular &quot;better Java&quot; languages removed the &quot;one-class-per-file&quot; restriction that Java put into place. Kotlin and Scala both allow any number of public declarations, even non-classes, to be placed in a single file, and they do the work of compiling that down to something the JVM acknowledges.</p>

CC BY-SA 4.0
2023-05-16T22:00:53.157Z
2023-05-16T22:00:53.157Z
Silvio Mayolo
CC BY-SA 4.0
2023-05-16T22:08:26.953Z
FWIW, this isn't uncommon in JavaScript -- you can just `require('is-odd')(3)` -- but JavaScript allows you to export whatever the heck you want.
Bbrk24
CC BY-SA 4.0
2023-05-16T22:14:49.583Z
There's a few things I want to say about this answer. (1) There's **tonnes** of examples of languages that have this pattern. Other than MATLAB/Octave, there's also Fortran, Julia and probably most programs used in scientific computing, which is my area. (2) It's **not true** that MATLAB forces you to split up several small public functions into "extremely tiny files". I often have all my functions in one file, or at least, many functions per file. (3) You said "forces a file to be in one-to-one correspondence with the language's notion of a "function"" but I was not talking about "forcing".
Nike Dattani
# Why does Python not allow us to call functions like other languages do?
Posted by: username_0 on 2023-05-16
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?
**Tags:** language-design language-comparison python matlab-octave functions
> **Comments:**
> **username_1**: Typically you would write `from get_xyz import get_xyz`, e.g. `from timeit import timeit` or `from 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 like `get_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*.  
> **username_2**: I said this in my answer as well, but can you name another language where files are functions? I've only ever seen this pattern in MATLAB and I've always found it, to be frank, incredibly silly (more so than forcing files to be classes).  
> **username_0**: @SilvioMayolo There's **tonnes** of examples. Other than MATLAB/Octave, there's also Fortran, Julia and probably most programs used in scientific computing, which is my area.  
> **username_1**: The example I'd give is in Javascript, you can `export default` a 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.  
> **username_0**: @kaya3 I edited my question in response to your comment, but SE only allows us to "ping" one user at a time in comments, so I only replied to Silvio's comment the first time :)  
> **username_1**: I see your edit and I do not think it addresses the problem with your question. `from get_xyz import get_xyz` does not make your code substantially longer than `import get_xyz`, which you already must be doing; it's the same number of lines, just one line is a bit longer, and it's not a particularly long line regardless. Even so, the question should be "why doesn't Python have a shorthand syntax for importing a single-function module?", given that the premise "why doesn't Python let me call the function this way?" is objectively false.  
> **username_0**: @kaya3 We don't have to run `import get_xyz` every time, if we already have the whole package loaded/installed in a different way. Also, the title of my question is not "why doesn't Python let me call the function this way?", it's longer than that, and when you include the part that you removed, it's no longer "false". Another title could be "Why doesn't Python let me do this without adding a `from get_xyz import get_xyz` line?" but we don't need to be so pedantic.  
> **username_1**: If there is a "whole package" then the module is not just a single function, so the premise is even more flawed. What would it mean to write `get_xyz(5)` if `get_xyz` is a module which exports *multiple* functions? As I said, the premise is objectively false, because Python **does** allow you to call a function like `get_xyz(5)` **if** the name `get_xyz` is bound to the function rather than the module that declares it. The same is true in any other language; you can write `get_xyz(5)` to call a function **if** the name `get_xyz` refers to that function.  
> **username_0**: @kaya3 Does it not depend on how the `__init__.py` is structured? Also, can you please stop being so aggressive by repeatedly saying that everything is "objectively false" and "even more flawed", etc.? The premise of the title "Why does Python not allow us to call functions like other languages do?" is true in my opinion because it can't be done like it can in MATLAB/Octave, Fortran, etc., without adding something like `from get_xyz import get_xyz`, so it **cannot** do it like we can in those languages.  
> **username_1**: "It can't be done without X" directly contradicts "it can't be done" ─ it can be done, with X. Your issue is that you think X is cumbersome, because it requires writing the name `get_xyz` twice in the import statement instead of once. But Python is not disallowing you from doing this. By framing your question in this way you make it impossible to answer without disputing the premise. You can write `get_xyz(5)` to call a function if `get_xyz` is the name of a function, you cannot if it is the name of a module instead. But why insist on having it be a module, if you want it to be a function?  
> **username_0**: @kaya3 I think that when you re-read your comments tomorrow, you might agree that you are splitting hairs. Again, I haven't said "It can't be done without X". I said "It can't be done the same way as it can in other languages", which I believe is true. I haven't made it "impossible" to answer without disputing the premise, because two answers have already been written, and if they dispute the premise, that's okay. Users are allowed to write answers that dispute the premise :)  
> **username_1**: @NikeDattani I've re-read both of our comments several times, and not changed my assessment. It's like if you asked "how come ketchup doesn't come out of the ketchup bottle, like in other restaurants?" when the bottle in your hand is mustard. OK, you took the bottle from where you think the ketchup *should* be. But it's not a ketchup bottle, and if you *had* taken the ketchup, you'd see it works the same as other restaurants' ketchup bottles. What you really want to know is why the bottles are in different places, but you're focusing on your ketchupless plate instead of which bottle you took.  
> **username_0**: I totally disagree with your ketchup analogy. There's a reason why I said "tomorrow" in my last comment. You're not going to see anything new here within a 30 minute time frame, and neither am I.  
---
## 2 answers
### Answer by username_2 on 2023-05-16. Score: 9
# This pattern is not as common as you think

It looks like you're referencing MATLAB there. MATLAB is the only language I know that forces a file to be in one-to-one correspondence with the language's notion of a "function". In my experience, when a language ties an existing construct to the source files making up your program, it just limits what you can do.

* MATLAB forces files to be public functions, and that forces me to split up several small but public functions into extremely tiny files.
* Java forces files to be public classes. This results in several awkward patterns where I have so-called "classes" containing only static methods.

The languages that have the most success leveraging the file system do so by creating a new construct in the language itself specifically *for* the notion of a file. Python, and many languages, calls these construct a "module". A module is not an existing language feature. It's a new thing that can essentially be defined as "the reification of a file within Python". As such, its needs are exactly built to suit files and are not shoehorning the filesystem into an existing model of "functions" or "classes".

It's telling that the two most popular "better Java" languages removed the "one-class-per-file" restriction that Java put into place. Kotlin and Scala both allow any number of public declarations, even non-classes, to be placed in a single file, and they do the work of compiling that down to something the JVM acknowledges.
> **Comments:**
> **username_4** (2023-05-16): FWIW, this isn't uncommon in JavaScript -- you can just `require('is-odd')(3)` -- but JavaScript allows you to export whatever the heck you want.  
> **username_0** (2023-05-16): There's a few things I want to say about this answer. (1) There's **tonnes** of examples of languages that have this pattern. Other than MATLAB/Octave, there's also Fortran, Julia and probably most programs used in scientific computing, which is my area. (2) It's **not true** that MATLAB forces you to split up several small public functions into "extremely tiny files". I often have all my functions in one file, or at least, many functions per file. (3) You said "forces a file to be in one-to-one correspondence with the language's notion of a "function"" but I was not talking about "forcing".  
---
### Answer by username_3 on 2023-05-16. Score: 2
There are a few reasons:

1. As you said, so no two namespaces have the same name.
2. Readability and clarity. The `module.function()` syntax provides clear information about where the function is defined making it more readable and clear, especially in big projects.
3. Modularity and Encapsulation. This helps organize code and improves its maintainability.
4. Explicit importing. It is a documentation method to show what's in the code and prevents function name collision between different modules.
5. Consistency and easy to use. It is also how you would go about getting attributes or object methods, making it more intuitive and easy to learn.
> **Comments:**
> **username_0** (2023-05-16): **(2)** I find `module.function()` unnecessarily verbose, rather than "more readable and clear" than `function()`. **(3)** How does it help here? **(4)** Is this not the same as (1)? **(5)** How is it more "intuitive" and "easy to learn" than allowing us to do function()? If you still think it's more intuitive and easy to learn, by how much? I picked up MATLAB way faster than I picked up Python. Within a week I could do more in MATLAB than I can do in Python after a few years (albeit not doing full-time Python programming during these years).  
---
