Format formatted output, the easiest pit to ignore

Recently, some students encountered a problem when using format to format strings. This problem is also a pit that everyone is very easy to step on in the process of using format.

The problem code is as follows:

Code:

Format formatted output, the easiest pit to ignore

 

Format formatted output, the easiest pit to ignore

 

error:

Format formatted output, the easiest pit to ignore

 

One, the basic use of format

Before officially stepping on the pit, let's review the use of the format method. Those who have known python, everyone knows that the format of a string is used to format a string.

So what is formatting?

Formatting is simply to output the string according to the set format

Format formatted output, the easiest pit to ignore

 

Use format to output personal information as above

format: Method one

Format formatted output, the easiest pit to ignore

 

format: Method two

Format formatted output, the easiest pit to ignore

 

format: Method Three

Format formatted output, the easiest pit to ignore

 

Two, easy to ignore pits

Code Thinking 1: What is the value of func after formatting here?

 

 

Answer: func ='get_data(10)'

For this question and answer 99.9% of the pass can answer the correct result, then look at the next piece of code

Code Thinking 2: What is the value of func after formatting here?

Format formatted output, the easiest pit to ignore

 

answer:

'get_data(<function add at 0x001F0810>)'

For this piece of code, many students answered func ='get_data(add)' without thinking about it, then it is actually not but this result

Three, analysis of format principle

format has no restrictions on the type of data passed in when formatting the output.

It means that the data you pass in the format can be of any type.

But when format is formatting, it is the string content returned by the __repr__ method of the obtained formatting object for formatting. In the above case, we passed add in format as a function.

Then the __repr__ of the function object returns the function description information that everyone has seen above.

Note: Not only functions, but objects other than basic data types are output in this way when formatting is used.

When you use format to format, you must pay attention to this point!

Code Thinking 3: Can the formatted function get_data(xxx) be executed through eval()

Format formatted output, the easiest pit to ignore

 

When the above code is executed, the error mentioned at the beginning of the article will be encountered.

Four, problem solving

The solution is as follows:

Format formatted output, the easiest pit to ignore

Recommend a software testing learning exchange group: 785128166, there are shared videos, interview guidance, test materials, mind maps, and videos in the group. They are all dry goods, you can download and watch. Mainly share test foundation, interface test, performance test, automated test, TestOps architecture, Jmeter, LoadRunner, Fiddler, MySql, Linux, resume optimization, interview skills, and actual video data of large-scale test projects. Use every minute and every second of your time to learn to improve yourself, and don't use "no time" to conceal your mental laziness! Try hard while you are young, and give your future self an explanation!

Public number: Programmer Erhei, after paying attention, you can receive a large amount of learning materials for free.

Good things should be shared with friends
 

Guess you like

Origin blog.csdn.net/weixin_53519100/article/details/114850880