(Star Python developers to improve Python skills)

Pea Flower cat

Built-in functions are a feature of Python, implementing many common operations in a minimalist syntax.

They are pre-defined in built-in namespaces, out of the box, wySIWYG. Python’s reputation as a newbie friendly language is true, and built-in functions play a key role in that.

For example, to find the length of the string x, Python writes len(x), and this works equally well for lists, tuples, and dictionaries, simply by passing in the corresponding arguments. Len () is shared.

It’s a minimalist philosophy: Simple is better than complex.

However, this is not the case in some languages. For example, in Java, the string class has a length method, and other classes have their own length methods, which cannot be shared. Each time it is used, it is called by class or instance.

To find the length of a string, run the following command:

saying = "Hello world!" Print (len(saying))#

Copy the code

In Java, it might be written as follows (for simplicity) :

String saying = "Hello world!" ; System.out.println(saying.length()); // Result: 12

Copy the code

Python uses a prefix expression, whereas Java uses a suffix expression.

In addition to finding lengths, some of Python’s built-in functions can also be found in Java. For example, if a numeric string s is converted to an Integer number, Python can use the int(s) function, while Java can use integer.parseint (s); Integer numbers are converted to strings. Python can use STR (I), and Java has string.valueof (I).

Python’s built-in functions are not bound to specific classes; they are first-class objects. Java “functions”, on the other hand, cannot exist without classes; they are merely appendages.

From an intuitive point of view, the Python expression seems to be superior. However, they are not comparable, because they are two sets of language systems, each with a unique category background, and cannot be easily reduced.

For example, the Latin alphabet is not superior to Chinese characters just because it is easy to draw strokes, because the alphabet (phonetic characters) is far inferior to Chinese characters (ideograms) when it comes to ideogram. Similarly, The Japanese borrowed the radicals of Chinese characters to create characters that were less elaborate, but also completely devoid of meaning.

By analogy, Python’s built-in functions have the beauty of simplicity, but they are missing some of their signification. Some people also like to point to this as a design flaw in Python when they question/bash Python.

This brings us to the question most discussed in this article: why is Python designed with len(x) as a prefix and not with x.len() as a suffix?

In fact, suffix design is also possible, using two Python methods for lists as examples:

mylist = [2, 1, 3, 5, 4]mylist.sort()print(mylist)   # [1, 2, 3, 4, 5]mylist.reverse()print(mylist)   # [5, 4, 3, 2, 1]

Copy the code

They are all called through list objects, not plucked from the built-in namespace. The semantics are pretty clear: sort and reverse myList.

As it happens, they also have two sorted() and reversed() half-brothers, which are prefix expressions.

mylist = [2, 1, 3, 5, 4]sort_list = sorted(mylist)print(sort_list)   # [1, 2, 3, 4, 5]reverse_list = reversed(mylist)print(list(reverse_list))   # [4, 5, 3, 1, 2]

Copy the code

Different ways of writing it, all doing the same thing (regardless of their side effects). Therefore, suffix grammar is not impossible, it must be a deliberate design.

Back to the previous question: why len(x) and not x. Len (x), and what design ideas come from Python?

Guido van Rossum, the father of Python, has explained this problem for two reasons:

  • For some operations, prefixes are more readable than suffixes — prefix (and infix) notation has a long history in mathematics, and its visual effects help mathematicians think about problems. We could simply rewrite the formula x*(a + b) as x*a + x*b, but the same thing would be clunky to do in a native object-oriented way.

  • When I read len of x, I know I’m trying to figure out the length of something. It tells me two things: the return value is an integer, and the argument is some kind of container. But when I read x.len(), I have to know in advance that some container X implements an interface or inherits a class that has standard len() methods. We often see this confusion: a class that doesn’t implement a mapping interface but has a get() or keys() method, or some non-file object that has a write() method.

After explaining these two reasons, Guido summed it up in one sentence: “I see ‘len’ as a built-in operation.” This isn’t just saying len() is more readable; it’s elevating len() altogether.

This is like saying that the line in ½ is a “built-in” expression in mathematics that doesn’t need to implement interfaces or anything like that. It already says “something over something.” Different types of numbers (integer, floating point, rational, irrational…) Sharing the same operator eliminates the need to implement a fractional operation for each type of data.

Elegant and easy to understand is Python’s design philosophy, best exemplified by the prefix expression of len().

Let’s look at slicing first. Probably the most common usage is “take the first n-bit element” or “take the last n-bit element from the i-th index” (the former usage is actually a special use of the starting bit of I ==). It would be very elegant if these two usages could be implemented without ugly +1 or -1 in expressions.

Using 0-based indexing, half-open interval slicing, and default matching intervals (which Python eventually adopted), the slicing syntax for the above two cases becomes very nice: a[:n] and a[I: I +n], the former being an abbreviation of a[0:n].

So len(x) beats x.len(), underpinned by a pure, yet profound design philosophy that reduces complexity to simplicity.

Since their invention, object-oriented programming languages have wanted to mimic the real world in which we live. But the poison of classes, interfaces, objects, and their methods sometimes blinds us to the nature of the world.

Table class has table class length method, chair class has chair class length method, endless, but the reality is really so? Can’t the method of finding length be a kind of independent object? It exists because objects exist, not because of a class.

So len(x) beats x.len(), which also shows Python’s insight into the nature of the world.

Finding the length of an object is an operation that exists independently of the object and is not a property or function of the object itself. From this perspective, we can see why Python designs built-in functions. Built-in functions capture the essence of the world.

These subtle discoveries are enough to make us fall in love with the language. Life is short. I use Python.

【 Author 】

Pea flower under the cat: a 985 college graduates, both geek thinking and humanistic feelings. Python cat is a personal public account dedicated to Python technology, data science and deep learning.

Recommended reading

(Click on the title to jump to read)

Fedora 32 will remove Python 2 and its packages

RHEL 8 does not use the default Python version

Microsoft embraces Python

Find this article helpful? Please share it with more people

Follow the “Python Developer” star to improve your Python skills

Good article, I’m reading ❤️