Python is not a great programming language
gist.github.com
gist.github.com
Yeah, I thought so when I was reading the list of "problems". In fact, many of those are features (e.g. lists & tuples being different, list comprehensions, lazy evaluation, distinction between maps/dicts and objects, ...) but the author doesn't actually understand them. I hate to sound so negative, I guess I just didn't realize how bad a programmer knowing (only/mainly) JavaScript causes you to be.
Either way, if the number of idiosyncracies in your language is confusing to junior developers it's absolutely something worth considering.
In python, I can have the
if __name__=='__main__': main()
at the end if a script, and easily import the script's functions into
- pytest for testing
- some other module for larger modules
- into a cli.py file to slap a Click interface on it.
What a great blend of flexibility while retaining coherence.
Why? Aiming for the lowest common denominators and the simplest systems tends to not create scalable long term solutions.
You also seem to really undercount what a properly trained junior developer is capable of understanding.
Java rode a couple of different trends at the same time: it provided a somewhat smooth upgrade path from C++, cross-platform compatibility, and an astronomical marketing budget.
Many languages in the top-10 that we believe are popular or mainstream got there by having one of platform lock-in, smooth upgrade from a prior language, or a killer, exclusive app. It's a lot less about what is good or objectively better and more about being in the right place at the right time.
I've heard some claim that Python got there by being slow and steady. At one point I think I would have agreed but it does have a killer app and that's in the analysis, scientific and machine learning communities.
> Too many other weirdo bits of magic syntax, like [list comprehensions].
I sadly have to agree with you.
Hey now - not all of us ECMA devs are total crap, but, I guess I know a fair bit of other languages. This guy is just totally ... I have words for it but I'd rather not get the mods on me.
I struggled with an appropriate way to say this but I think he's super pretentious and just sounds frustrated with learning a new language. I mean, we've all been there but typically we don't post ignorant BS to HN/GitHub about it... Every time I jump into a new language I'm always stubbing my toes on things I find "awkward", only to learn later that there's usually a reason people design languages certain ways.
If he wants to shoot his social media reputation in the foot as a developer then so be it I guess =/
But while Python itself I think provides an awesome developer experience, django seems less than awesome, and it's taken a while for me to feel proficient in Django.
That all said, I do love me some Python though! I use it as a general purpose tool to get semi-complex things done that are more scripting/job oriented vs. needing the orchestration that Django brings to the table. You could say it's a "cog" for me vs. the overall machine/engine.
It's on my TODO list to get into some other Python framework libraries because I've seen lots of promising ones pop up since Django, if anyone has suggestions of what to poke my nose into I'm all ears!
I think Python is pretty awesome, but to pretend that it's super clean is a bit of a stretch.
(a,) for a single-elem tuple-- avoiding colliding with (a) as a paren'd expression-- is wonky, too.
x = 1,
x = (1,)
Of course, then there's no way to express the empty tuple using commas, which is where () comes in.Using (a,) is the belts-and-suspenders way of saying "this is a 1 element tuple".
Also "foo['bar'] returns a KeyError", then saying sometimes this is resolved by "getattr(foo, 'bar', None)" -- the split between attributes and items are one of the best things about python.
I don't know why list comprehensions are "magic syntax", and you can't complain about having to type list(map(...)) and then complain that you don't like list comprehensions!
"You have to cast your data back to a list/tuple after using enumerate() and map()." -- I don't know what that means
"Different syntaxes for lists and tuples." -- Again, what? They're different types!
How is r'a\nd' less "goofy" than String.raw`a\nd` in JS or any other language with prefixed strings?
Python is clearly not for everyone nor for every problem, but in my opinion it is a great programming language.
Lists and tuples have different syntax in a weird and surprising way. List syntax is straightforward, just square brackets and commas. Tuple syntax pretends to be list syntax with parentheses but it's actually only about commas except when it isn't.
Typically you write `(a, b)`, but the parentheses are only for precedence, and can be left out if it's unambiguous: `a, b`. You can write a 1-list as `[a]`, but a 1-tuple is `(a,)` because `(a)` is just `a`. An empty tuple on the other hand is `()`, without any commas, and with parentheses doing something other than precedence. It's very ugly.
I can't think of a better way to fit it into the rest of the syntax but I still count it as a flaw.
In this case the problem stems from the overloading of parentheses. There aren’t any more paired delimiters in ascii available unfortunately. Perhaps t[] could have been chosen, but as you see it isn’t exactly elegant either.
I think he means you don't get a list/tuple back after applying enumerate and map. That's actually lazy evaluation and it's a plus. If you just want to iterate over the values, why create a list that you are going to throw away? Twice the effort for nothing.
I agree with you that this reads as someone expecting JS behavior in Python.
{'a':1, 'b':2, 'c':3}
or dict(a=1, b=2, c=3)
I find the second syntax easier to read and write for dicts where all of the keys are static strings that are valid Python variable names.I'm assuming that the author has a problem with the fact that those two functions return a generator and not a list.
As a Python coder going back to 2008, I can see the point the author makes in isolation.
I disagree with the title though. Even though I mostly write Rust, Go, and C anymore, I am fine with digging into Python as needed. I cringe at thoughts of using Ruby or JS
Things I don't like about ruby: - two string types, symbol and string, with string being the mutable by default one.
This means given a random thing back from an api, you don't know whether to do thing[:id], thing["id"], or thing.id
This has been acknowledged as a pain point by Matz, which is why in ruby 3 strings will be immutable by default.
- Simultaneously too many names for things and not enough.
Is it .length or .size or .capacity (probably not capacity)? is_a?, kind_of?, or instance_of?? Why can I do .select or .keep_if but not .filter?
- Too many function types.
Do you want a method, a block, a proc, or a lambda? There are subtle differences between each, so choose wisely. I'll note that python suffers from this too (method, function, lambda, comprehension).
- Too much emphasis on magic
Novice rubyists get frequently bitten by all the advanced (and very hard to google) ruby concepts. How do you know what arr.map(&:id) is without already knowing that it's calling symbol.to_proc? How about $1 $? $! (if you know what all these do, you're a better rubyist than I am).
Since the author of the referenced post criticizes django for being too magical, try rails. In addition to the names of files mattering a ton, there's the routes DSL, the migrations DSL (which is not well specified in the guides), and ActiveSupport, which you only realize you're using when it's gone.
- Horrible error messages
undefined method :[] for nil:NilClass (when you try to get something out of a hash and it's nil) cannot convert Symbol into Integer (this is on an array being returned where a hash is expected) Also, if you get a stack trace, it's completely inscrutable. Python's stack traces (at least ipython's) show you both the line number and the line in question (often with context).
Care to show proof? I don't see this a lot.
The code of the official Django site for example has 8 occurences: https://github.com/django/djangoproject.com/search?q=super+_...
This is a mistake based on thinking in Javascript: in JS the index operator is effectively the same as the dot operator, for example foo[“bar”] == foo.bar. In Python those are different. [] corresponds to get in dicts, and . corresponds to getattr in objects.
My personal biggest gripe with Python is that there isn’t a better story for typing as I’m spoiled by TypeScript and static languages that have a better development experience and prevent certain classes of mistakes.
Not to mention, a lot of software treats type hints very differently. PyCharm is able to do powerful inference that MyPy can’t do.
I will say that I do like the act of writing a pro and con list for a language or tool though(which is why I read the linked doc), and I often push co-workers to do so when they're suggesting things. Sometimes it shakes a bad choice out just with the simple act of jotting down a 5-point list.
Unless, of course, there actually is an ideal language out there. In which case I retract my point ;)
This person clearly doesn’t understand programming languages so well. Sorry to make this response an “ad hominem” one, I should revoke the claims. But it’s impossible if they don’t understand basic concepts of programming languages.
For example, complaining that a dictionary key must be place in quotes. It’s not that “the key needs quotes”, but that you’re using “a string” as a key. In fact in Python, you can use any immutable object as a key (tuples, for example). They clearly come from Javascript, Python works differently (and arguably better), and they’re complaining that it doesn’t work the way they’d like it to work.
Then complaining about list comprehensions as a “weirdo” thing. Clearly not understanding the functional paradigm and the beauty of expressions (see Smalltalk for the ultimate example of beauty and expressiveness).
Any hashable object, technically. Especially with custom objects, the two (immutability and hashability) don’t necessarily have to overlap, although it’s often a bad idea to have hashable, mutable objects
Agreed. However, there's a trick to use strings without quotes:
dict(foo=1, bar=2) == {'foo': 1, 'bar': 2}Here are a couple of more, from someone who has worked for years in a number of languages:
- few limitations means you can easily make a serious mess, which means you are more dependent on good programmers to avoid the mess.
- lack of typing information isn't "free solo" hard but it certainly makes life a lot less pleasant.
One of the projects I work on has switched to full type hinting along with heavy mypy¹ usage, and it has become an absolute pleasure to work with. Along with hypothesis², I can't recommend mypy enough. It must be said that retrofitting either to an existing project is a lot of work though.
1. http://www.mypy-lang.org/ 2. https://github.com/HypothesisWorks/hypothesis
Years ago the lack of visible types was often flouted as a benefit (i.e. terser code, less boiler plate, easier for learners to understand etc).
I eagerly await the addition of optional "scope hints" of { and } ! ;-)
Sarcasm aside, now that there are type hints and python programmers need to type as much as Java/golang/c# programmers, why not just write in those languages?
Genuine question - what are the benefits of starting something new in python (assuming all things being equal - i.e. equal knowledge in java/golang/c# and no legacy reasons forcing you)? Why would anyone pick writing type-hinted python Vs a fully explicitly typed compiled language?
Scope hinting is simply called braces; `from __future__ import braces`.
> Genuine question ...
IMO assuming all things are equal seems to miss how things actually are. The pluses still fall on Python some times, and other languages at other times. Things do vary; developer availability, library availability, target system support, certification story, tooling, even curbside appeal can be legitimate on some occasions.
I'm trying not to pick on your individual language examples, as firing digs seems to be missing the point we're discussing. Or perhaps it is the very point, as two of them wouldn't have even been in my list.
Whole other debate on benefits and drawbacks if this sort if thing however..
And then there’s Bython... https://github.com/mathialo/bython
I've been pleasantly surprised how shallow the "general python rabbit-hole" is.
If i can express this in words, one of the ways I like to judge a language when I program: i think of something a computer could theoretically do, then I look for ways to express it in that language. How many independent jumps I have to take down the conceptual rabbit-hole before I get to the solution is a nice little arbitrary metric.
Does python do everything the way I'd do it? No. You get over yourself and just accept that's the way it is in this language.
Once I've done that, so far most problems in python have been pretty shallow: do this arbitrary thing and then this arbitrary thing and you're done.
Compared to some of my past languages where you have to go 4 or 5 levels deep with N compulsory but conceptually irrelevant steps, it's pretty damn good. Makes for a reasonable quick pathway to actually getting anything done...
Purely my subjective opinion.
Called uniform function call syntax, an I idea I first saw in Nim and found really cool, but then found really dumb. Can't remember the reasoning for both opinions, which is a strong indicator of "probably doesn't matter".
For a while I was entertaining a thought regarding mutability and returning copy's. Like foo.bar() would mutate, bar(foo) return a modified copy. Don't know if that concept has a name or if it actually makes sense at all.
From my main perspective (scheme programmer): I am constantly amazed by the bad code python programmers write in scheme. Take a nice recursive function, sprinkle it with set! (which leads to boxing and general slowness in many implementations) and if that wasn't slow enough for you, wrap it in a call/cc to be able to return from arbitrary places in the function. As a bonus, forget to discard the captured continuation to make your program use insane amounts of memory.
Then proceed to complain on Reddit.
Generators should be treatable as lazy lists in the end, and lists should have a common interface whether they are lazy or eager. Someone should figure out a way to have us write code at a higher abstraction level and have same interface for lazy vs eager data structures.
...but it's not gonna happen in a dynamic language like Python. And I can't say I like the "solution" of having the entire language be lazy like Haskell either :|
We're stuck with "casting generators" for now, I guess, but it really does suck!
With that said...
> In theory a "sufficiently smart language" should have a feature to understand that `thing[3]` can be translated to "call `next()` 3 times
This might be a newbie trap, because next() isn't the same as indexing. What happens if I perform `thing[7]` followed by `thing[5]`? Should performing `thing[7]` put 1-6 in memory and turn the object into a generator-list hybrid?
You're right here. Probably can't work like that since generators are too general, you can't expect them to be rewindable or to not have side effects... prob you'd need a more specialized concept like a "lazy list" that would be a subtype of generator with some extra restrictions that would make it possible to implement the "hybrid" structure as an implementation detail without changing semantics.
Anyway... it would be too much work and probably would turn into a footgun.
They seem to exist only to do lots of stuff in one single line of code. You end up with totally impenetrable unreadable perl-esq garbage write-once-read-never code that is too clever for its own good. And people say python is easy to learn and good for beginners...!
A better approach would be something like Java Streams/.net Lync/RxX pattern IMO. Explicit, clear, no magic, logical.
g = [i for i in list if x == 3] -> g = []; for i in list: if x == 3: g.append(i);
How could that be improved? That's 3-4 LOC minimum in any other language
My main grip is python's ternary operators, since the True value is evaluated before the condition, if you are doing ternaries on things that might throw exceptions the False value has to come first
value = 0 if key not in dic else dic[key] * 5
rather than (throws indexerror if key isn't in dic)
value = dic[key] * 5 if key in dic else 0
dic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"}
> "How could that be improved? That's 3-4 LOC minimum in any other language"If I'm understanding the comprehension correctly,
(into {} (filter (fn [[k v]] (and (get other-dic k) (= v "bar"))) dic))
Though, for readability, I'd likely write it as: (->> dic
(filter (fn [[k v]]
(and (get other-dic k)
(= v "bar"))))
(into {}))
Legibility is in the eye of the beholder.(for) k, v in dic.items()
which is just
k, v = (<key>, <value>) for every key value pair in the dictionary
I posted due to your claim regarding all other languages necessitating increased verbosity. I should have left it lie, as I didn't intend to promote a language war, just to post a counter example. My apologies.
dic = dic.where((k, v) => k in other_dic and v == "bar").todict()
dic.iter().filter(|k, v| other_dic.contains(k) && v == "bar").collect();
it's one line, though I'd format it as 3 for readability (list comprehension is hard to read and functional style composes better.
https://news.ycombinator.com/newsguidelines.html
For example, instead of putting someone's article down as sophomoric, you could explain what's different and possibly better about Haskell list comprehensions.
All these politeness comments are a speed bump that distracts from the real issues.
I used to think similarly about this to what you express, because I've always enjoyed reading about the sort of discourse in which devastating wit is exchanged. But eventually I realized that it doesn't translate into this context at all. This is a case of 'the medium is the message'. When you have millions of people who don't know each other all potentially interacting at the same time, the dynamics are so different as to be incommensurable with, say, small debates, literary journals, elite social events, and other places where the groups are small and highly cohesive. Here are some previous explanations about this:
https://hn.algolia.com/?dateRange=all&page=0&prefix=true&sor...
https://news.ycombinator.com/item?id=15378909
https://news.ycombinator.com/item?id=9378899
https://news.ycombinator.com/item?id=7906377
https://news.ycombinator.com/item?id=7742471
The bottom line is that having a forum like HN be open to everybody comes at the cost of some blandness.
> The syntax for classical inheritance. Half of each Django app is super().__init__(args, *kwargs). At least you don't have to pass arguments to super anymore.
Yes, inheritance is hard to do well, but that's generally true. Much better to prefer composition (https://en.wikipedia.org/wiki/Composition_over_inheritance)
> Too many magic __double-underscore__ methods and properties that you have to just memorize.
Better to just start using http://attrs.org or dataclasses so you don't have to do all the manual work. Classes without boilerplate.
> Too many top-level built-in functions that (a) you have to just memorize, and (b) get really ugly. You end up with stuff like list(map(...)). I haven't used so many nested parentheses since my early days in PHP. Guido's explanation makes sense in theory, but is really annoying in practice.
I agree, there should be a pipeline operator |> like F# has. A similar proposal has been made for JS. https://github.com/tc39/proposal-pipeline-operator
> Too many other weirdo bits of magic syntax, like [list comprehensions].
List comprehensions are good, they just take 5 minutes of getting used to.
> Django specifically is so full of magic words, and its documentation is so convoluted, that I've basically given up on documentation altogether and just look at the Django source code now.
Pyramid has a much more principled design IMHO. https://trypyramid.com/
> Needing to put dict property names `{'in': 'quotes'}.
Or use `dict(foo=5)`. Python mappings can contain non-string keys, so `{5: 1.2, 6: 3.4}` maps ints to floats.
> You have to cast your data back to a list/tuple after using enumerate() and map().
Don't use map, use comprehensions.
> Different syntaxes for lists and tuples.
They are different objects, why would they have the same syntax?
> foo['bar'] returns a KeyError, so you have to do foo.get('bar')... or in some cases getattr(foo, 'bar', None), but not in others because getattr and .get are different things.
Python is a different language from Javascript.
> You can't just tack on flags to /regular_expressions/ig.
Yeah that's annoying.
> All the goofy string literals: f' ', u' ', r' ', etc.
That's a good thing, not a bad thing.
> Pipfile does not work that well.
Poetry works better. https://poetry.eustace.io
Yes. I have my disagreements with Python, but those are not it. The article author is mostly complaining about ways Python differs from Javascript.
- Agree that the mechanism for talking about parent classes wasn't very good. Multiple inheritance usually adds complication without adding much necessary functionality. That's not just a Python problem. It's a leftover from viewing objects through an "A is-a B" lens, one of the dead ends of early AI.
- Python has too much gratuitous dynamism. Any thread can find and mess with any code and data in another thread. The implementation has to support that, which knocks out many valuable optimizations. The language model, and the original implementation, use "everything is a dict", which implies "slow".
- One consequence of the above is Python's terrible one thread at a time thread system, with the "global interpreter lock". The "multiprocessing" hack to get around that is ugly and uses too much memory, since each subprocess has its very own Python system instance. To some extent, the "async" add on is yet another hack to get around the limits of threading.
- Another consequence is a tendency to call C code where Python performance is terrible. The C code has to carefully obey the rules of the Python system. Mostly it does.
- Optional typing is a marginal idea, but unchecked marginal typing is just weird. Language design seems to be converging on implicit static typing - result variables are automatically typed whenever possible. Go, Rust, and now C++ (with "auto") took that route.
- And, of course, the botched Python 2 to 3 transition set Python back for a decade.
On the other hand, Python exceptions work out well. A reasonably sane exception hierarchy helps. Although the one for 2.x was better than the one for 3.x; the 2.x one made a clear distinction between external problems ("Environment errors") and internal problems.
The "with" clause system plays well with exceptions, and nested exception failures unwind correctly. It's far better than Go's "defer". C++ and Rust try to handle this sort of thing with RAII, which never handles trouble in a destructor well.
> Needing to put dict property names `{'in': 'quotes'}.
Now that's a part I like.
Does the author not realize that you can use (almost) anything as a dict key?
a = 123
b = 'foo'
d = {a:456, b:123, 2.3:'2.3'}
print(d)
>>> {123: 456, 'foo': 123, 2.3: '2.3'}You can do that in JS too, it's just coerced to string, so you can do e.g.
{foo: "bar"}
where foo is not a variable, but assumed to be the string foo. In Python you can't, because you couldn't tell if it's foo the string, or a reference to an existing (or non-existing) foo variable.That said, in JS, not only you're limited to using (real or coerced) strings as keys to Objects, but you also need to use the square bracket:
{[foo]: "bar"}
syntax, so it can tell that you need it to use the value of the variable foo as the key (else it will understand {"foo": "bar"}).The article has a silly premise - I don't know anybody who would claim it's a great programming language. For the moment, it's just the most practical in certain areas (notably data science and machine learning.)
[0] https://docs.python.org/3/library/typing.html#typing.Union
[1] https://mypy.readthedocs.io/en/latest/more_types.html#functi...
> The philosophy of "one correct way to do things."
I miss those days. T_T
> A huge ecosystem of good third-party libraries.
Python also has a huge ecosystem of huge ecosystem managers, which is less than ideal.
> Half of each Django app is super().__init__( * args, * * kwargs)
Haven't used Django, but if I were providing a suite of classes people were extending, I'd provide lifecycle hooks.
Using super().__init__ is not a syntax problem as much as it's a semantics problem, because it's so fragile.[1]
> Too many magic __double-underscore__ methods and properties that you have to just memorize.
Yup, dunders are the ugly consequence of duck-typing. Maybe Python could tuck them away in some kind of traits system.
> Too many other weirdo bits of magic syntax, like [list comprehensions].
But the Python syntax is weird:
[elem for outer in iterable for inner in outer]
Which is essentially: for outer in iterable:
for inner in outer:
result.append(elem)
It's backwards. The PEP[2] doesn't explain why, but looking at the JS syntax[3] it does seem less worse.Other weirdo syntax:
while some_condition():
if check_a_thing(elem):
break
else:
print("never broke from loop")
The meaning sort of makes sense if you think of `while` as an extension of `if`.But I would have expected this:
for x in y:
do_a_thing()
else:
handle_empty_case()
Or, really, a more descriptive keyword.> You have to cast your data back to a list/tuple after using enumerate() and map().
I thought it was the coolest thing when generators got full support in py3k, but it's horribly broken.
This does not raise an exception:
x = list(some_generator)
y = list(some_generator)
y will be empty, which is a completely silent failure. The same is true for iterators. And everyone's been bit by strings being iterable[4].Sometimes it's useful to do this:
i = iter(some_generator)
x = next(i)
for elem in i:
...
But Python should not:1. make iterators be iterable.
2. allow spent generators to be reused without exception.
3. make strings be iterable.
(All of which could be bypassed with a method call when you really want that behavior.)
> foo['bar'] returns a KeyError, so you have to do foo.get('bar')
LOL, only javascript devs could not have noticed the hours they've spent tracking down the source of some mysterious undefined. Python definitely got this one right.
[1]: https://fuhm.net/super-harmful/
[2]: https://www.python.org/dev/peps/pep-0202/
[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
[4]: https://mail.python.org/pipermail/python-3000/2006-April/000...
[5]: Wait, am I talking about the OP or this comment?
Django's ecosystem (mainly DRF and django-filters) makes it very productive for me, but I find Django core to be lacking for developer ergonomics in certain areas.
However, Pypy is a Python interpreter that is written in (a compiled subset of) Python 2.
There are two paths to take when one encounters something new; adapt and learn, or reject and mock. This sounds like the latter.