Difficulties encountered by python

Source: Internet
Author: User

Difficulties encountered by python

Due to the shortage of front-end resources, my back-end system was too late to wait for its front-end, so I had to write it on my own. I started learning about html and js and wrote several instances according to the official tutorial of vue. js. I cloned a good vue. js template from github for a week. Although the interface is a bit ugly, it finally passes through all the services, during which cross-origin, jump, cookie and other issues are tortured not to be required. If you have time, this article will summarize the problems encountered during the development of vue. js.

To put it bluntly, this week, the blogger basically did not touch the python code, but Fixed a bug, which is very interesting. Bug Description: The returned results of this request always carry the results of the previous request. After the service is restarted, the bug is temporarily fixed and will appear later. The first reason is the database cache problem. However, we didn't enable a special cache mechanism for mysql, so it's not a cache problem. The second reason is whether or not the results take effect immediately after the related SQL operations are executed. If the results take effect after troubleshooting, this is not the case. So we had to debug the code. The last one located the parameter passing Problem of a method.

Display simplified version of the problem code

#!/usr/bin/python# -*- coding: utf-8 -*-class TEST(object):    def test(self, info, info_list=[]):        info_list.append(info)        return info_listif __name__ == '__main__':    a = TEST()    print a.test(1)    print a.test(2)    print a.test(3)

I believe most people share the same expectations for output. When writing code, I think the output result is

[1][2][3]

But the actual output result is

[1][1, 2][1, 2, 3]

It seems that the bug is found, and the info of the previous request is retained in the returned results of this request. After the service is restarted, info_list is reinitialized to [], so the bug disappears temporarily. I found the problem, but why does it happen. When we instantiate this method, info_list is initialized to [] and will not be initialized again later. It can be understood that info_list is a global variable in this method. to modify its value, you can only pass in a new info_list to overwrite it when calling the method, that is to say, change the method call to. test (1, []) will not have this problem. Not all data structures have this problem. Only the variable data structures such as list and dict have this problem. tuple, string, and int do not exist. Therefore, we recommend that you use the style when initializing list and dict in the method.

 

#!/usr/bin/python# -*- coding: utf-8 -*-class TEST(object):    def test(self, info, info_list=None):        if info_list is None:            info_list = []        info_list.append(info)        return info_listif __name__ == '__main__':    a = TEST()    print a.test(1)    print a.test(2)    print a.test(3)

 

The output is as expected.

 

 

[1][2][3]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.