A Python User’s Response to “Python Criticism from a Julia Perspective”
A colleague recently brought an article titled “Python Criticism from a Julia Perspective” to my attention. I didn’t really agree with any of the criticisms in the post, so I thought I would reply point-by-point.
Managing Virtual Environments
Managing virtual environments in Python is super simple. The whole thing only requires a few commands.
Setting up a virtual environment
python3 -m venv my_env
This command uses the venv
module (which comes with Python, i.e., nothing additional needs to be installed) to create a directory called my_env
in your current directory that will manage your virtual environment.
Activating/deactivating/deleting a virtual environment
To activate a virtual environment, from within the directory where you created the virtual environment, run:
source my_env/bin/activate
Your terminal will add (my_env)
to the beginning of your command prompt to let you know that you’re in the virtual environment. To leave a virtual environment:
deactivate
If you want to delete a virtual environment, you can just remove its directory:
rm -rf my_env
Installing packages in the virtual environment
While inside your virtual environment (i.e., (my_env)
should be prepended to your command prompt):
pip3 install numpy pandas matplotlib Pillow IPython
Saving a virtual environment
While inside your virtual environment:
pip3 freeze > my_env.txt
my_env.txt
will contain a list of packages and their versions that were installed in your virtual environment.
Recreating a saved virtual environment
While outside your virtual environment (i.e., after running deactivate
), and assuming my_env.txt
is in your current directory:
python3 -m venv new_env
source new_env/bin/activate
pip3 install -r my_env.txt
Don’t use Anaconda
Notice that I haven’t referred to Anaconda at all. That’s because not only is it entirely unnecessary, it often makes managing Python installs/environments/packages quite painful—to the point that one of the first questions I ask people who are having issues with Python is “Are you using Anaconda?”. Don’t use it.
Documentation
I’ve honestly never heard someone complain about Python’s documentation before. In my opinion, the functionality of print
, append
, and split
are pretty obvious, so it’s not clear to me why an example in the documentation is necessary (and note that the documentation in the screenshot for split
does say “using sep
as a delimiter string”). Further, most people learn to use these functions before they even really know how to navigate help
and the Python documentation (there are also a bazillion examples on the internet…). For functions that are more complicated, the documentation often does contain code examples, e.g.:
from scipy.spatial.transform import Rotationhelp(Rotation.from_rotvec)
Truthy and Falsy
“Truthy” and “falsy” values are a design choice that have been adopted by a wide variety of programming languages. While this can lead to surprising behavior in some edge cases, it’s usually not a problem, particularly in scientific computing where you’re generally comparing numbers in conditional statements. More importantly, the example in the article is not complete. While:
if not xs:
print("collection is empty")
works for determining if a collection is empty, the more natural way to write it (and the way many Python programmers would do so) is:
if len(xs) == 0:
print("collection is empty")
which works just as well.
Function Discoverability
I think most Python programmers just use Google to find functions/packages for doing things. I honestly couldn’t relate to this section because the workflow was so foreign to me.
Generality
Slicing
I’ve never had trouble getting slices or ranges out of Python lists/NumPy arrays, so it was hard for me to follow this particular criticism.
Anonymous functions
The anonymous function discussion was pretty confusing. The article showed an example that wasn’t correct Python syntax, acknowledged you’re supposed to use lambda
in Python, but then never provided a correct example. Here’s the correct way to write that anonymous function:
f = lambda x: x + 3
f(1)
which doesn’t seem that complicated to me. As for with
, it doesn’t have anything to do with anonymous functions; it’s for context management, which is a fairly technical concept, but it serves a clear purpose (more information here if you’re curious).