In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the code style of EAFP and LBYL in Python". In the operation of actual cases, many people will encounter such a dilemma, 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!
1. EAFP style
Python has his own philosophy, of which EAFP is the most famous. Its full name is:
Easier to Ask for Forgiveness than Permission
Forced translation means that it is easier to seek forgiveness than to get authorization.
For students who come into contact with this EAFP noun for the first time, it may be difficult to understand the meaning of this sentence.
It doesn't matter, let's take a look at what EAFP-style code looks like, and you can get it right away.
In the following code, I use the [key] method in try to get a dictionary value value, but because profile does not have a key for age. So an exception is thrown.
Profile = {"name": "Ming Brother"} try: print (profile ["age"]) except KeyError: print ("Key is not exist")
Like this, the code style of believing that there is nothing wrong with the code itself, letting it be executed, and then capturing it to deal with it when there is a problem is called EAFP style.
Personally, I call it crash-oriented programming.
2. LBYL style
The full name of LBYL is:
Look Before You Leap
Translate, that is, take a look before you jump, this is a relatively conservative way of writing. This way of writing may allow you to write a lot of if judgments to avoid various scenarios where errors may occur.
Or take the above code as an example, using the LBYL style to write, it is like this
Profile = {"name": "Ming GE"} if "age" in profile: print (profile ["age"]) else: print ("Key is not exist")
3. Which style do you choose?
Use a vivid example to describe their difference.
There is a pair of small friends together to visit a famous mountain, because the mountain is relatively steep, there are all kinds of school cliffs.
Partner A, is a relatively bold and open person, he put on his own parachute, so when playing, he has no scruples and does not watch the way, and there is a parachute to save his life if he accidentally falls off a cliff. This is EAFP style.
And partner B, is a more conservative person, he did not wear a parachute, so every walk to see if there is a cliff ahead. This is LBLY style.
Although EAFP is a coding style recommended by Python, in more cases, these two styles will exist in your code at the same time. The purpose of the code style is to enhance the readability and robustness of the code. In some scenarios, the EAFP style is easier to read, and in some scenarios the LBLY style is easier to read, so there is no need to worry about which style to use. The specific situation should be analyzed.
That's all for "what are the EAFP and LBYL code styles in Python?" 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: 207
*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.