281 points · by pabs3 · 4y ago · 157 comments
I remember back then it was said the design philosphy was something like there should be one obvious way to code something correctly. In opposite to Perl where the philosohpy was that human thoughts can take weird ways and you should be able to put your thoughts to code directly. (Both characterizations from memory.)
Today I hear little from Perl. And with new syntax added to Python in every version I start to wonder how far away Python is drifting away from that original characterization above.
I'm not sure if today that really matters though. I will admit I don't know much about Perl but Perl's death vs Python's life probably might have some influence from design choices but a lot of it just has to do with the libraries you have access to with Python. I know as a scientist, Python is the game in town, that has more to do with why I use it than design.
It was really only during the break-away adoption by the data science community (and other mass audiences) that started adding pressure to bloat the language. The adoption of 3rd party libraries like numpy, which are so critical to modern Python, also destroyed the community's "batteries included" philosophy.
Older Python felt a lot like using FreeBSD, where the whole system is cohesive and designed to work together, whereas modern Python feels a lot more like Linux – a bunch of disparate systems that try to work well together (in the best of times).
Both approaches have their strengths, and the language has certainly improved in some areas, but I prefer the older style.
In any case, it is fascinating to see how mainstream Python is now. Maybe this change in mindset was required for it to take over the world. We've come so far since Paul Graham cited the language as esoteric and its usage a high signal of competence (http://www.paulgraham.com/pypar.html).
You are right and your FreeBSD vs Linux analogy really hits the target.
Thanks to the advancement of hardare and the development of libraries like Numpy, Python has moved from it's scripting niche and became a global, general purpose language.
Was that an error? If we look at the "batteries included" philosophy and the poor state of Python's package management after all these years...I'd say yes.
Syntax integration would be great. It's such a hassle to learn two syntaxes and two data/object models to work with numpy -- as inevitably your code converts from Python objects <-> numpy objects at some point (singletons!), and it makes learners' lives much more difficult than needed.
I think Python 3 almost killed Python, too, and for the same reasons. The developers behind 2to3 saved the community, in my opinion, as did the folks who made the painful decision to publish a Python 2 end-of-life date sufficiently far enough into the future that people still using the old runtime had both a sense of urgency and plenty of time to complete their migrations. I finally made the leap to Python 3 around 2016. I'd like to claim that I jumped instead of being pushed, but the reality is that by then, people were developing new stuff I wanted badly on Python 3 exclusively.
Frankly, I believe the terrible state of affairs on CPAN was part of the problem, but it was initiated by the "there's more than one way to do it" mentality. If I wanted to do something where it would be a good idea to have a library for that task, I would have to search many candidates, each of which did sixty to eighty percent of whatever, then see which were long-abandoned, did what they actually managed to write match what I needed, etc.
Certainly not. Perl was always the language that had all the libraries. Python only started to get a large ecosystem way after Perl was doomed.
> The number of independent primitive concepts has been minimized in order that the language be easy to describe, to learn, and to implement. On the other hand, these concepts have been applied “orthogonally” in order to maximize the expressive power of the language while trying to avoid deleterious superfluities
Viewing through this lens, the Python Zen's "obvious way" refers to concepts in the language, and not the wider ecosystem which includes libraries.
Sure, but tests in the submission (not talking about further discussions) are only about the language. And with so much new syntax added over the years I doubt that there can be only one obvious way for many problems.
"I doubt" is just my feeling. I have not make any efforts to demonstrate this by examples. And it wouldn't be easy for me because I am not very fluent with many of the newer features of the language.
So, there can and should be lots of ways to do things. But always one obvious way, a Happy Path that's not obscured at all or whatever.
> There should be one – and preferably only one – obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
The first is that I think actually achieving a hard-mode form of that quote and maintaining it through the lifetime of the language is impossible in practice and theory. (Even "mostly" living up to the quote is an achievement.)
The second is that I am unsure about the obvious qualifier. Certainly I can recognize pythonic and non-pythonic constructs in python; but whether the difference is actually obvious or just a result of experience combining with community consensus I can't say.
The third is that I don't think the language is Pareto efficient between readability and most other variables. For instance, I've had code criticized as non-pythonic that was just as readable as the offered pythonic version, but had greater run-time efficiency. (To be fair, this is only an issue if you define the 'pythonic' way to be the 'one obvious way'.)
All the other things built on top that people use Python for are very interoperable because they use the same base data type.
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!It is from a different age, when intelligent people like Tim Peters still had influence and there was more of an academic atmosphere. These days it is about warming chairs, getting power, eliminating your enemies and speaking at conferences.
NaMeSpAcES ArE ONe HONkInG gReAt IdEa
Oh you want to print to stderr hmm?
def module_from_path( ctx, name, path ):
### thx to https://stackoverflow.com/a/50395128/7568091 ###
### thx to https://stackoverflow.com/a/67692/7568091 ###
import importlib
import importlib.util
spec = importlib.util.spec_from_file_location( name, path )
module = importlib.util.module_from_spec( spec )
sys.modules[ spec.name ] = module
spec.loader.exec_module( module )
return importlib.import_module( name )
Details may change any time without notice. The complexity here is a consequence of Python's design decision to treat module names as identifiers, not as quoted strings. The import system is full of such poor choices (dot/relative imports, `__init__.py`, `*.pyc` / `*.pyo` files littering one's source directories, and so on)It would be nice if that flaw could be fixed, as I like coding in python in general
All the Perl libraries for doing certain things (db access, web stuff, process management) were "actually battle tested" and worked well even if they were utterly unreadable. Python looked a lot nicer but when you tried to do any real work with it everything seemed to leak memory or give you continuous paper cuts - and it was a lot slower.
I made the switch to Python eventually but it took a while.
https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to...
(It's a joke due from this page: http://james-iry.blogspot.com/2009/05/brief-incomplete-and-m..., actual motto is "there should be one -- and preferably only one -- obvious way to do it")
In fact, I think this makes it so all print() calls by default write to stderr:
>>> import sys, functools, builtins
>>> real_print = print
>>> builtins.print = functools.partial(real_print, file=sys.stderr)
>>> print("Hello")
HelloPerl
print STDERR "foo"
Python print(file=sys.stderr, "foo")
Those look fairly similar. I prefer that print() is obviously a function call and file is a named parameter so the purpose is clear.warn("foo")
In Python you have to at least:
- remember not to forget to import sys - remember the file= syntax for the print statement - type about 10 extra characters
Bunch of annoying stuff when you are hurriedly debugging something...
Which requires politics, churn, adding non-essential features and covering up mistakes by the old boys.
Just check values in sys.version_info then throw an exception, it's so much more obvious what you're doing.
If your __init__.py didn’t have any version-specific syntax, then yes you could check the interpreter version before importing the rest of the files.
# -*- python >= 3.1 -*-
The trouble is, that wouldn't work on the old versions!Yeah, a bit late to address the issue. Would have been helpful in the 2->3 transition.
Partially because people avoid Python for things like installers and simple sharable single file scripts...because of this. There are ways around it in many other scripting languages. And a fairly good number of popular "single file scripts" that get shared, like mysqltuner (perl), adminer (php), etc.
Instead of checking whether proper version is used, why not just ensure it runs on right now (and perhaps includes needed dependencies)
It's actually a fairly practical approach, where you are controlling where the code is going to bomb out, and including a helpful comment that will show in the stack trace / exception.
With Perl, for example, you can have a BEGIN block to check for versions, and it works as expected, even if there's code in the main body that's too new for whatever version of Perl is running. Python doesn't have that ability unless your code is split across more than one file and using conditional imports. Some BEGIN block type functionality for python seems like it would have been useful to me.
import sys
sys.version_info
if not sys.version_info[:2] == (3, 1):
raise Exception("You need Python >= 3.1")
else:
from your_module import whatever
I really hope no one's seriously using this repo to get any ideas. if sys.version_info < (3, 1):
... In [2]: sys.version_info
Out[2]: sys.version_info(major=3, minor=10, micro=1, releaselevel='final', serial=0)
In [3]: sys.version_info < (3, 10)
Out[3]: FalseWhat would be your suggestion to do this for single file scripts?
The point is obviously to let if fail, but clearly explain why it fails. This is obviously not meant for software distributed to users that are proficient in Python.
I guess it's useful for the kind of scripts you write and post in pastebin, or share on a web forum or whatever. Think a wrapper script that downloads and patches Wine so that it can run a certain exe or whatever. These users have never heard of pip, and you don't want to publish this stuff to pypi anyway. You just want to post it in a code block on forums.obscureindiegame.com so the three Linux users there can join the effin' game already!
Or so I would guess. Never felt a need for something similar myself.
I know there are probably other answers (I guess writing binaries in Go?), but given how nice Python is for glue code when it works, I do wish for something like `python --config-file pyproject.toml my-script.py` that would figure out requirements, install those "somewhere", ensure the right python version (installing if necessary) and then run the script in a consistent environment (PYTHONPATH being reset, for example).
I know there's a lot of tools for packaging Python code into their own binaries, but I think we could probably get pretty far with some built-in "install and run" tooling that doesn't start with "decide where to put a virtualenv"
FWIW, Python is not shipped with macOS anymore:
https://www.macrumors.com/2022/01/28/apple-removing-python-2...
At the very least you need Xcode Command Line Tools.
Perl still comes with MacOS 12.3. And Perl would be a decent language to bootstrap a working Python with working certificates, etc.
Though Apple has said all scripting languages will be removed from the default install eventually.
"Scripting language runtimes such as Python, Ruby, and Perl are included in macOS for compatibility with legacy software. Future versions of macOS won’t include scripting language runtimes by default, and might require you to install additional packages. If your software depends on scripting languages, it’s recommended that you bundle the runtime within the app."
>I guess writing binaries in Go?
Then you get the fun of signing, or README language to guide the user around adding an exception. They sure are making bootstrapping hard. I wish they would pick at least one stable scripting environment to bundle...lua maybe?
If the state of the art in Perl is still "shell out to curl or whatnot" then that is really not portable or stable compared to Python's included urllib--SSL errors or not.
HTTP::Tiny ships with Perl, including 5.16. I'm not sure if MacOS Perl comes with a working IO::Socket::SSL you would need for https.
Edit: See if this works or not:
perl -MHTTP::Tiny -E 'say HTTP::Tiny->new->get("https://www.google.com/")->{content}'It's OK for me if there is a single binary that is "guaranteed" to work that also needs to be installed (for example "you have to install nix/tox"), similar to people having to install a JVM (which mostly works relative to Python).
And yeah you can use curl for web requests. But when your problem then becomes "make a web request, take a chunk out of that" you're looking at curl _and_ jq. And clean error handling...
I know how to write shell scripts, but I think I hit their abstraction ceilings way too early, especially if I'm looking to write something that is maintainable by other people on the team. I know people talk about how every machine will have some shell, but when you think about it a bit more all the "nice" shell utils are not installed by default.
All the more power to people who can write clean shell scripts. I try to, but am not good at it. But I know how to write Python (or JS, or Java, or C if someone demanded I do it...)
https://gist.github.com/travisbhartwell/f972aab227306edfcfea
Yes, you have to install nix, but you _only_ have to install nix.
bcc @dummy
@dummy:
With the special case of an extra cycle just on the off chance the bcc is at the end of the current page. It indeed leaves no side-effects but I don't know if people would really seriously call it a nopWikipedia has table of them for different architectures https://en.wikipedia.org/wiki/NOP_(code)
if True: def f(x): return x
Is this just because Python doesn't want to encourage extreme terseness? Or is it also because there would be problems or grammar ambiguities were this to be allowed?"Only the [indented] form of a suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which if clause a following else clause would belong:
if test1: if test2: print(x)
" $ python3.5 since-3.6.py
File "since-3.6.py", line 1
0_0 # Python >= 3.6 is required
This error means python 3.5 knows the syntax is valid in 3.6 which is plausible, but why would `python2.4 since-3.6.py` have a useful error message?Edit: the comment is included in the source and python prints the whole line
0_0 # use python 3.6 or greaterIt doesn't. This relies on the interpreter printing the whole line that contains a syntax error, including the comment on it.
$ python2
Python 2.7.17 (default, Feb 27 2021, 15:10:58)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0_0 # Python >= 3.6 is required
File "<stdin>", line 1
0_0 # Python >= 3.6 is required % python2 since_3.6.py
File "since_3.6.py", line 1
0_0 # Python >= 3.6 is required
^
SyntaxError: invalid syntax
% python2 --version
Python 2.7.18 $ python2 --version
Python 2.7.18
$ python2 py36.py
File "py36.py", line 1
0_0
^
SyntaxError: invalid syntaxI'm not sure why this guy is doing it the hard way.
I've been using something similar to
exec '' # Python 2.X is required
in a few projects stuck on Python 2.Seeing something like this in the wild would be a strong nope from me.
It's too clever by half.
???