Python's maximum recursion depth error "maximum recursion depth exceeded while calling a Python object"

Source: Internet
Author: User

Today, when writing a crawler, found a strange thing, using the Str method to cast a BeautifulSoup object into a string error, the hint is "maximum recursion depth exceeded while calling a PythonObject ", meaning roughly" when the object is called exceeds the maximum recursive depth "

The error is as follows:

  Traceback  (most recent call last):  file  "<stdin>",  line 1, in <module>  file  "C:\Python27\lib\site-packages\bs4\element.py",  line 1045, in __str__    return self.encode ()   File   "C:\Python27\lib\site-packages\bs4\element.py", line 1055, in encode     u = self.decode (Indent_level, encoding, formatter)   File  "c \ python27\lib\site-packages\bs4\element.py ", line 1126, in decode     Indent_contents, eventual_encoding, formatter)   File  "C:\Python27\lib\site-packages\ bs4\element.py ",  line 1195, in decode_contents    formatter))    File  "C:\Python27\lib\site-packages\bs4\element.py", line 1126, in decode   ......    file  "C:\Python27\lib\site-packages\bs4\element.py",  line 1126, in decode     indent_contents, eventual_encoding, formatter)   File  "c \ python27\lib\site-packages\bs4\element.py ", line 1195, in decode_contents     formatter))   File  "C:\Python27\lib\site-packages\bs4\element.py", line  1098, in decode    text = self.format_string (Val, formatter)   File  "C:\Python27\lib\site-packages\bs4\element.py",  line 163, in format_ String    output = formatter (s)   File  "C:\Python27\lib\ Site-packages\bs4\element.py ",  line 120, in substitute_xml    ns,  entitysubstitution.substitute_xml)   File  "C:\Python27\lib\site-packages\bs4\element.py",  line 104, in _substitute_if_appropriate    if  (Isinstance (ns, navigablestring) RuntimeError:  Maximum recursion depth exceeded while calling a python object

And then more amazing is that I use the Ptpython did not error, directly through the.

In fact, the reason is that the recursive call in Python is limited, you can use the SYS module in the Getrecursionlimit method to view, that is (want to delve into the classmate can Google search, here to provide the author of the search https:// cyrusin.github.io/2015/12/08/python-20151208/)

Sys.getrecursionlimit ()

Open terminal to run Python, you can see the default limit value is 1000

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/8B/C3/wKiom1hXscDBcpqVAAAKssVwL6Y070.png-wh_500x0-wm_3 -wmp_4-s_698534623.png "title=" Qq20161219180614.png "alt=" Wkiom1hxscdbcpqvaaakssvwl6y070.png-wh_50 "/>

The default limit in Ptpython is 2000, which makes it easy to explain why Python runs directly at the maximum depth recursion error and Ptpython works correctly.

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8B/C3/wKiom1hXtBqjFGRnAAADxmCmvtk125.png-wh_500x0-wm_3 -wmp_4-s_803356705.png "title=" Qq20161219181829.png "alt=" Wkiom1hxtbqjfgrnaaadxmcmvtk125.png-wh_50 "/>

Then to solve this problem, there is a natural set of get (of course, there are other methods such as to reach the depth limit to do the corresponding processing this is not in line with the author's current needs, so do not repeat, there is a need for students to Google Baidu a bit), So the way to set the maximum depth limit is to setrecursionlimit, as to how much you set the value for yourself.

Sys.setrecursionlimit (2000)

At this point, the problem solved!


This article is from the "Cloud Inn-leyex Study Notes" blog, please be sure to keep this source http://leyex.blog.51cto.com/4230949/1884041

Python's maximum recursion depth error "maximum recursion depth exceeded while calling a Python object"

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.