Share 6 hidden python features

  • 2020-06-15 09:26:03
  • OfStack

This site has introduced you to some of python1's rarely used features before, but this time we share 6 hidden python features to learn about.

In python's design philosophy, there is a rule that says, "Simple is better than complex". Simple code is better than complex code, which is why python is recommended as a beginner's language. Many people who are new to python tend to be influenced by other languages, such as those who have learned java before and write python like java1. For example, when designing a class in java, we often define the get and set methods for the internal variables. This is an important way to ensure encapsulation, but it is not recommended in python. The internal variables in python are underlined with a single word (such as ES21en.name), but you still have access to the private variables. How do you say? Using conventions in python, I wouldn't force you to say you can't access this variable, but you'd better not. If you need to do something else with a variable, use the @parameter decorator for get and set encapsulation, as direct access to internal variables can be an error. I'm going to talk about classes and objects later.

If you open the python interactive environment and type import this, you will see the following:


The Zen of Python, by Tim Peters
Beautiful is better than ugly.  Beauty is better than ugliness 
Explicit is better than implicit.  Clarity is better than obscurity 
Simple is better than complex.  Simple is better than complex 
Complex is better than complicated.  Complex is better than messy 
Flat is better than nested. Flat is better than nested 
Sparse is better than dense.  Spacing is better than compactness 

These are python's design philosophy, with 1 more to follow, and are best followed when writing python code.

The first feature.

If you were asked to write 1 piece of code to indicate that a is greater than 2 and less than 10, most people would use a > 2 & & a < 10, right? In python you can just use 2 < a < 10.


a = 5
#  It can be 
print(2 < a < 10)
#  Or you could do that 
print(10 > a <= 9)

True
True

Another trick that many of you probably know is to swap values in place instead of using intermediate variables.


a = 2
b = 3
print(a, b)
#  Direct exchange 
a, b = b, a
print(a, b)

2 3
3 2

The second function.

We often need to use the for loop to iterate over values in a sequence and then do something. In other languages you might write:


a = ['a', 'b', 'c', 'd', 'e']
for(int i = 0; i < len(a); i++):
print(a[i])

Many people in python would write this, using range to generate 1 sequence for the length of a, and then traversing.


a = ['a', 'b', 'c', 'd', 'e']
#  right a Length of use range generate 1 Sequence, and then iterate over it 
for i in range(len(a)):
print(a[i])

a
b
c
d
e

You can actually write it this way, just use the enumerate method, which returns the index and the value of the sequence.


a = ['a', 'b', 'c', 'd', 'e']
#  right a Length of use range generate 1 Sequence, and then iterate over it 
for i in enumerate(a):
print(i)
#  Or it 
for index, value in enumerate(a):
print(index, value)

(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
0 a
1 b
2 c
3 d
4 e

The third function.

In general, the loop statement and the conditional statement are unrelated. if is followed by else and for is followed by in. You can also follow the loop statement with else. for followed by else means run the loop and then execute the statements in else.


a = 5
#  It can be 
print(2 < a < 10)
#  Or you could do that 
print(10 > a <= 9)
0

In addition to the for loop which can be followed by else, while and try/except can also be followed by else.

The fourth function.

As anyone who has ever used a dictionary knows, if you need the value of a key in a dictionary, you can use d['key'] to get it and throw an exception if key doesn't exist, which is not good, but if you use ES105en.get ('key'), None will be returned by default when key doesn't exist, so you don't have to worry about whether or not an exception will occur. Instead, d.get ('key', 0), the second parameter specifies the value to replace if key does not exist.

The fifth function.

Regular expressions are a pain in the neck, and it would be nice if I could annotate them so I know what I'm writing. In Python you can do this.


a = 5
#  It can be 
print(2 < a < 10)
#  Or you could do that 
print(10 > a <= 9)
1

a = 5
#  It can be 
print(2 < a < 10)
#  Or you could do that 
print(10 > a <= 9)
2

The sixth feature.

As mentioned in the previous iterator and generator article, the iter() function can generate 1 iterator, after which you can use the loop or next method to produce values. In fact, iter also accepts a second parameter, which is used to stop if the second parameter is encountered during the iteration. Here's an example:


def seek_next_line(f):
for c in iter(lambda: f.read(1),'\n'):
pass

In the code above, loop in from f and end the read if \n is touched.

Other techniques, such as using generator expressions, using unpacking methods, and so on, have been covered before and will not be covered here.


Related articles: