In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to talk to you about the three good ways to write Python, which many people may not know. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Here Xiao Xin recommends three good ways to write Python, which can help you become a better programmer.
1. Docstring
Docstring is the abbreviation of Python document string. Docstring is the first statement of a defined module, function, class, or method in triple double quotation marks. This is the smallest example of docstring in a function.
Deffoo (): "This function doesnothing." Passprint (foo.__doc__) # Thisfunction does nothing.
The documentation string for the function should contain (one line) a brief introduction to the purpose, and the following paragraphs describe the function calling convention. There are a variety of styles, but this is one of my favorite templates:
Defsum_of_squares (nums): "" Compute the sum of squares of a list of numbers. Args: nums (`list`of `int` or `float`): a `list`of numbers. Returns: ans (`int`or `float`): Sum of squares of `nums`.Raises: AssertionError: If `nums` contain elements that are not floats nor ints. "" Try: ans = sum ([x**2for x in nums]) except: raiseAssertionError ('Input should be a list of floats or ints.') Return ans
Rawdocstring.py initiated by GitHub
2. F-string (formatted string)
You may be used to using the following command to format a string, that is,% or format ().
Name = 'World''Hello% s'% name # Hello World'Hello {} '.format (name) # Hello World
Abandon them. Once you need to print multiple variables in a longer string, the code will quickly become confusing and difficult to understand. In any case, these formatting methods are not simple.
Pythonf-string is a game-changing tool introduced by Python 3.6. This is a readable and advanced string format syntax that embeds an expression in a string. This is done through the statement f'{expr}', where the expression is enclosed in curly braces within the f string. The expression begins with f, which precedes the single quotation mark.
Example:
Name = 'World' print (f'Hello {name}') # Hello World
You can write any grammatically valid expression in curly braces. You can also call functions in expressions!
A = [1mai 2.2 is 3] print (f'Sum of squares of {a} is {sum_of_squares (a)}') # Sum of squares of [1mai 2.2 Magi 3] is 14.84
3. Naming convention
Naming things is one of the most difficult things in computer science. You ran out of ideas. But do not know how to name temporary mediation variables But you're not the only one who can't name it.
Although difficult, there are some naming conventions in Python to "narrow" the selection of variables. They help enhance code consistency, readability, and reusability.
Therefore, if the letters themselves have no meaning, you should no longer use a single lowercase letter to name all variables, such as a, x, and so on. In addition, you should name them with meaningful, easy-to-understand and easy-to-recognize words, such as replacing uspr with user_profile.
Here are 6 tips for naming things:
1. Avoid naming things with a single letter, such as O, I, l. The reason is obvious.
two。 Both variable and function names should be lowercase.
3. Words in variable or function names are separated by an underscore.
4. Private variables (for example, within a class) may start with an underscore
5. Words in category names should be concatenated and capitalized, such as MarioKart.
6. Constant names should be capitalized, such as GOLDEN_RATIO.
This list is not absolutely exhaustive. Naming variables can be said to be one of the hardest things to learn in programming. Therefore, reading other people's code and learning their naming methods on GitHub is the best way for beginners.
After reading the above, do you have any further understanding of the three good ways to write Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.