Home  >  Article  >  Backend Development  >  Python - How to solve when writing code takes too long?

Python - How to solve when writing code takes too long?

零下一度
零下一度Original
2017-07-03 16:13:282936browse

The coding specification of

Python requires that the length of each line should not exceed 80 characters. Then there is a question. What if I really need to write more than 80 characters of code in one line?

Python statements can easily divide one line into multiple lines. For example, the following two ways of writing are equivalent:

l = [1, 2, 3, 4, 5 , 6]
l = [
1, 2, 3,
4, 5, 6
]

If a line in a Python statement is too long, use this method It's easy to write one line into multiple lines, but what if I just have a string that is longer than 80 characters?

For example, I have a string like this:

s = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras magna turpis, viverra et cursus eget, efficitur sed eros. Curabitur finibus ultricies nisi, nec convallis nunc gravida sed. Nam ac facilisis eros, quis tincidunt turpis. Vestibulum quis mi quis justo faucibus condimentum. Aliquam accumsan ultricies orci id mollis. Mauris faucibus dignissim luctus. Aenean lacus nunc, ultricies ac ante eu, fringilla accumsan nisi. Cras et nunc posuere, mattis mi at, rutrum felis. Etiam volutpat neque sodales condimentum consectetur.'

Someone said that Python doesn’t have a string enclosed in three quotation marks? In this way, you can write multiple lines, for example:

s = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aliquam bibendum purus at nibh rutrum dapibus.
Aliquam quis sem sagittis, rutrum magna quis, gravida quam.
Sed porta ex nec leo elementum, a efficitur neque ornare.
Ut tristique nunc vitae ultrices molestie.
"""

But writing this way will bring about a problem, that is, a newline character will be added after each line. What if my string only has one line? This would not be appropriate.

I think the better ways are as follows:

1. Add escape character at the end of the line

s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
Aliquam bibendum purus at nibh rutrum dapibus.\
Aliquam quis sem sagittis, rutrum magna quis, gravida quam.\
Sed porta ex nec leo elementum, a efficitur neque ornare.\
Ut tristique nunc vitae ultrices molestie."

2. Use parentheses to hide Type conversion

s = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    "Aliquam bibendum purus at nibh rutrum dapibus."
    "Aliquam quis sem sagittis, rutrum magna quis, gravida quam."
    "Sed porta ex nec leo elementum, a efficitur neque ornare."
    "Ut tristique nunc vitae ultrices molestie.")
# 注意:行末没有逗号

Personally think that although the first method can solve the problem, it is too ugly. I recommend the second method.

The above is the detailed content of Python - How to solve when writing code takes too long?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn