by TekMol · 5y ago · 0 comments
The syntax
I was reluctant to to meaningful whitespace for a long time. But after doing some projects in Python I have to admit the code looks so much better than C style languages with all the {, } and ;sAnd it has one giant plus over PHP:
The module system
PHP went down the wrong road (of insanity) by deciding that the way to avoid naming conflicts is to use longer classnames. Because that is what namespaces in PHP are. Comfort tools to make your class names longer.There is still one giant benefit of PHP over Python:
Performance
Currently, PHP is about 6x faster than Python.Let's hope that the Python devs will focus on performance so we get the best of all worlds at one point!
Personally, I would only consider languages very close in popularity to the leading one when deciding on the language for a new project.
Still, I don't think I would choose Visual Basic to build the next big thing though :)
The namespace feature is more of a comfort tool to make those long names shorter.
Python's competition isn't PHP; it's Typescript.
[1, 2, 3, 4].map(x => x * x).filter(x => x < 10)
Python's lambdas and list comprehensions are quite restrictive in comparison to functional style. filter(lambda x: x < 10, map(lambda x: x * x, [1, 2, 3, 4]))
But even for more complicated logic, it also doesn't seem like it's that burdensome to just throw a name on the lambda: def square(x):
return x * x
def less_than_ten(x):
return x < 10
filter(less_than_ten, map(square, [1, 2, 3, 4]))
Which I find to be more readable/maintainable.That's probably the most underrated feature of python. Decisions about how the language should function like this which seem "restrictive" from one perspective, and yet force you down the avenue of good decision-making for readability/maintainability. Sure, it's possible to write terrible python, but you're generally cutting against the grain to do it.
Python's problem isn't a lack of lambdas, it's a lack of consistency; for some things you use functions and for others methods.
You can express the above as:
[x * x for x in [1, 2, 3, 4] if x < 10]
Which in set notation is {x^2 : x in (1, 2, .., 4), x < 10}.
x = [1, 2, 3, 4]
x = map(lambda x: x*x, x)
x = filter(lambda x: x < 10, x)
x = list(x)
Or is there a more elegant way? >>> (lambda x: x+1 if \
... True \
... else x+x)\
... (10)
11
:pIn a 100KLOC codebase, 99% of the performance issues are going to come down to 5 or 10 lines of code.
Don't get me wrong, I'm extremely happy that MS is funding a multi-year project to improve Python performance, and I think it will confer enormous benefits to the ecosystem. And frankly I think that it's absolutely needed.
But for me it would also be weird to choose languages based on some notion of average performance. Like sure, a line of Go might be faster than a line of Python on average, but if the entire bottleneck of your codebase revolves around one line of regex then what is doing a rewrite actually going to accomplish?
80/20 rule? Probably. Then 80% of CPU time is spent in 20K lines of code.
But 1%? I don't think so.
A web project of 100KLOC will be a shit ton of features and CPU time will be spent in hundreds of different places.
Another issue is what I would call "code depth". Even if you find a few lines deep down in a framework or library that makes your specific use case slow - what can you do? Patch the framework? That comes with a ton of issues on its own.