In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is the only effective standard to measure the quality of python code". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
About naming
When giving names to variables, functions, classes and packages, you should be worthy of the name and know the meaning by name. If the name needs to be supplemented with comments, it is probably not a good name. For example:
Int d
Def bar ():
Pass
Instead of having built-in names such as id, sum, len, etc., you can choose a business-related name, such as account_id.
Replace constant values with searchable constant names. For example, if there is a paging parameter with a size of 10 per page, we can define a constant PAGE_SIZE=10 that can be used elsewhere. If you write it as 10 alone, it will be troublesome to find it when you want to modify it later.
Get_data (10)
Vs
PAGE_SIZE = 10
Get_data (PAGE_SIZE)
The name is not the best, only better, this time did not find the right name, the next time you find the right name to use refactoring to change it, but for the exposed API, change the name should be careful.
About function
The function should be short, the function should only do one thing, and do it well, not two things at once, keep it simple and stupid. There is no strict definition of how many lines are short. If you can split what a function does into 12345 and other steps, then it does not one thing but five things.
Take a crawler as an example: crawling data through a url link and saving it to the database can be divided into several steps:
Initiate a network request and get the data
Clean the data
Save the database
In other words, this operation can be split into at least three functions, and each step may involve fewer steps, such as encapsulating parameters when sending a request, including request header parameters, proxy parameters, query parameters, and so on. If all the logic is written in one function, it looks like a piece of shit. Breaking each small function into smaller functions clearly reflects not only the code logic but also your programming ideas.
Function parameters are also as few as possible, ideally no parameters, followed by one parameter, followed by two parameters, try to avoid more than three parameters, unless you have a good reason to use three parameters. What if there are too many parameters? Find a way to abstract the parameters into objects.
In order to eliminate redundancy and reduce repetition in the function, many times you do similar things, such as getting the paging of the data list of various models, you can abstract the common code into common logic.
About comment
When it comes to comments, we all think it's a good thing, but it doesn't beautify bad code, so don't rely too much on comments to explain the intent of the code, but try to explain the logic of the code through the code itself. Useless code comments usually have
Nonsense annotation
Def get_all (cwd):
Get_dir = os.listdir (cwd) # iterate through the current directory to get a list of files
For i in get_dir:
Sub_dir = os.path.join (cwd,i) # add the file obtained in the first step to the path
If os.path.isdir (sub_dir): # if it is still a folder, recursively call
Get_all (sub_dir)
Else:
Ax = os.path.basename (sub_dir) # if the current path is not a folder, put the file name in the list
Result.append (ax)
Print (len (result)) # counts the list
This code is truncated from github, and all the comments are interpretations of api and have no other meaning.
A long string of commented-out code
Def merge_cookies (cookiejar, cookies):
If not isinstance (cookiejar, cookielib.CookieJar): raise ValueError ('You can only merge into CookieJar') if isinstance (cookies, dict): cookiejar=cookiejar _ from_dict (cookies, cookiejar=cookiejar, overwrite=False) # elif isinstance (cookies Cookielib.CookieJar): # try: # cookiejar.update (cookies) # except AttributeError: # for cookie_in_jar in cookies: # cookiejar.set_cookie (cookie_in_jar)
Always worried that one day the code will be changed back, reluctant to delete. If your code doesn't already use a version control system like git, you can keep it for the time being. But now it's not nice to go out and say hello without git, right?
Misleading comments, which are the most deadly, were originally written, but the logic of the code changed dramatically a few months later, but the comments remained in the original version.
Having said that, some comments are still necessary, such as the documentation of the function, which is used to explain the function of the function. If your function name is not self-explanatory, it is the second best way to go with the comment. Another kind of comment is the interpretation of intention, for example:
Try:
Current_position = o.tell ()
Except (OSError, IOError):
# This can happen in some weird situations, such as when the file
# is actually a special file descriptor like stdin. In this
# instance, we don't know what the length is, so set it to zero and
# let requests chunk it instead.
If total_length is not None:
Current_position = total_length
The comments here explain to the reader what scenarios such exceptions occur and how to handle them. This is a way to tell the reader what the author is thinking.
TODO comments are also a commonly used comment, for example, some places show with a temporary alternative, you can use the # TODO tag when you will optimize it in the future, and now most IDE supports TODO annotation highlighting.
That's all for the content of "what is the only valid criterion for measuring the quality of python code?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.