In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the method of implementing class f-string in python? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. How to format a string in python
There are three methods of python formatting string, which are using%, format and f-string, respectively. In terms of convenience, it is improved one by one, especially f-string, which greatly simplifies the writing of string formatting code. The following example is an example of using three ways to format a string.
Print ("% s% d years old"% (name, age))
Name ='Li Lei'
Age = 14
Print ('{name} this year {age} year old '.format (name=name, age=age))
Print (f'{name} this year {age} years old')
They have the same formatting effect, and the end result is
Li Lei is 14 years old.
Using the syntax of f-string, the code is obviously more concise, which aroused my interest to see if I could achieve a function implementation similar to that of f-string.
I define a function and hope that it will achieve a similar effect to f-string
Name ='Li Lei'
Age = 14
Def my_fstring (format_str):
Pass
String = my_fstring ('{name} this year {age} years old')
Print (string) # Li Lei is 14 years old and 2. Get the field name that needs to be populated and formatted
To achieve the above effect, first, you need to parse the string, here I use regular expressions
Import re
Pattern = re.compile ("{. *?}")
String = "{name} this year {age}"
Result = pattern.findall (string) # ['{name}','{age}']
Args = [item [1:-1] for item in result] # ['name',' age']
Using regular matching, parse out that there are name and age in the string that need to be replaced.
3. Get the values of name and age
Although you already know that strings need to be replaced and populated with name and age when formatting, how do you get the corresponding values for these two fields? The method I think of here is to get the frame when the function my_fstring is called, and to get name and age through the f_locals of frame.
Def my_fstring (format_str):
Pattern = re.compile ("{. *?}")
Result = pattern.findall (format_str) # ['{name}','{age}']
Args = [item [1:-1] for item in result]
Frame = sys._getframe ()
F_back_frame = frame.f_back
Value_dict = {key: f_back_frame.f_locals.get (key) for key in args}
Return format_str.format (* * value_dict)
Inside the function, the current frame is obtained first through the sys module, and then the frame of the upper layer is obtained through f_back. The f_locals attribute in the frame of the previous layer contains the name and age variables. Finally, the string is formatted using the format method of the string.
There are still many defects in the implementation of my_fstring function, which can not be replaced, and it is not necessary to replace f-string, but this research process makes me have a more in-depth understanding and learning of python function calls.
This is the answer to the question about how to implement class f-string in python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.