@lcf33 @bestony @osu-zxf @zxy-wyx @rakino @wyxplus @Gordon-Deng @hongsofwing @shipsw @chai-yuan 这些文章已经超时了。如果你还希望继续完成,请重新申请。
9.3 KiB
5 reasons why I love Python
These are a few of my favorite things about Python.
I have been using Python since it was a little-known language in 1998. It was a time when Perl was quite popular in the open source world, but I believed in Python from the moment I found it. My parents like to remind me that I used to say things like, "Python is going to be a big deal" and "I'll be able to find a job using it one day."** **It took a while, but my predictions came true.
There is so much to love about the language. Here are my top 5 reasons why I continue to love Python so much (in reverse order, to build anticipation).
5. Python reads like executable pseudocode
Pseudocode is the concept of writing out programming logic without it following the exact syntax and grammar of a specific language. I have stopped writing much pseudocode since becoming a Python programmer because its actual design meets my needs.
Python can be easy to read even if you don't know the language well and that is very much by design. It is reasonably famous for whitespace requirements for code to be able to run. Whitespace is necessary for any language–it allows us to see each of the words in this sentence as distinct. Most languages have suggestions or "best practices" around whitespace usage, but Python takes a bold step by requiring standardization. For me, that makes it incredibly straightforward to read through code and see exactly what it's doing.
For example, here is an implementation of the classic bubble sort algorithm.
def bubble_sort(things):
needs_pass = True
while needs_pass:
needs_pass = False
for idx in range(1, len(things)):
if things[idx - 1] > things[idx]:
things[idx - 1], things[idx] = things[idx], things[idx - 1]
needs_pass = True
Now let's compare that with this implementation in Java.
public static int[] bubblesort(int[] numbers) {
boolean swapped = true;
for(int i = numbers.length - 1; i > 0 && swapped; i--) {
swapped = false;
for (int j = 0; j < i; j++) {
if (numbers[j] > numbers[j+1]) {
int temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
swapped = true;
}
}
}
return numbers;
}
I appreciate that Python requires indentation to indicate nesting of blocks. While our Java example also uses indentation quite nicely, it is not required. The curly brackets are what determine the beginning and end of the block, not the spacing. Since Python uses whitespace as syntax, there is no need for beginning { and end } notation throughout the other code.
Python also avoids the need for semicolons, which is a syntactic sugar needed to make other languages human-readable. Python is much easier to read on my eyes and it feels so close to pseudocode it sometimes surprises me what is runnable!
4. Python has powerful primitives
In programming language design, a primitive is the simplest available element. The fact that Python is easy to read does not mean it is not a powerful language, and that stems from its use of primitives. My favorite example of what makes Python both easy to use and advanced is its concept of generators.
Imagine you have a simple binary tree structure with value
, left
, and right
. You want to easily iterate over it in order. You usually are looking for "small" elements, in order to exit as soon as the right value is found. That sounds simple so far. However, there are many kinds of algorithms to make a decision on the element.
Other languages would have you write a visitor, where you invert control by putting your "is this the right element?" in a function and call it via function pointers. You can do this in Python. But you don't have to.
def in_order(tree):
if tree is None:
return
yield from in_order(tree.left)
yield tree.value
yield from in_order(tree.right)
This generator function will return an iterator that, if used in a for loop, will only execute as much as needed but no more. That's powerful.
3. The Python standard library
Python has a great standard library with many hidden gems I did not know about until I took the time to walk through the list of all available functions, constants, types, and much more. One of my personal favorites is the itertools
module, which is listed under the functional programming modules (yes, Python supports functional programming!).
It is great for playing jokes on your tech interviewer, for example with this nifty little solution to the classic FizzBuzz interview question:
fizz = itertools.cycle(itertools.chain(['Fizz'], itertools.repeat('', 2)))
buzz = itertools.cycle(itertools.chain(['Buzz'], itertools.repeat('', 4)))
fizz_buzz = map(operator.add, fizz, buzz)
numbers = itertools.islice(itertools.count(), 100)
combo = zip(fizz_buzz, numbers)
for fzbz, n in combo:
print(fzbz or n)
A quick web search will show that this is not the most straight-forward way to solve for FizzBuzz, but it sure is fun!
Beyond jokes, the itertools
module, as well as the heapq
and functools
modules are a trove of treasures that come by default in your Python implementation.
2. The Python ecosystem is massive
For everything that is not in the standard library, there is an enormous ecosystem to support the new Pythonista, from exciting packages to text editor plugins specifically for the language. With around 200,000 projects hosted on PyPi (at the time of writing) and growing, there is something for everyone: data science, async frameworks, web frameworks, or just tools to make remote automation easier.
1. The Python community is special
The Python community is amazing. It was one of the first to adopt a code of conduct, first for the Python Software Foundation and then for PyCon. There is a real commitment to diversity and inclusion: blog posts and conference talks on this theme are frequent, thoughtful, and well-read by Python community members.
While the community is global, there is a lot of great activity in the local community as well. Local Python meet-ups are a great place to meet wonderful people who are smart, experienced, and eager to help. A lot of meet-ups will explicitly have time set aside for experienced people to help newcomers who want to learn a new concept or to get past an issue with their code. My local community took the time to support me as I began my Python journey, and I am privileged to continue to give back to new developers.
Whether you can attend a local community meet-up or you spend time with the online Python community across IRC, Slack, and Twitter, I am sure you will meet lovely people who want to help you succeed as a developer.
Wrapping it up
There is so much to love about Python, and now you know my favorite part is definitely the people.
I have found kind, thoughtful Pythonistas in the community throughout the world, and the amount of community investment provide to those in need is incredibly encouraging. In addition to those I've met, the simple, clean, and powerful Python language gives any developer more than enough to master on their journey toward a career in software development or as a hobbyist enjoying playing around with a fun language. If you are interested in learning your first or a new language, consider Python and let me know how I can help.
via: https://opensource.com/article/19/10/why-love-python
作者:Moshe Zadka 选题:lujun9972 译者:译者ID 校对:校对者ID