SoFunction
Updated on 2024-11-13

Problems and solutions for python recursive calls returning None

Today, when I was doing python to get emails, I needed to call the parser function recursively to parse the emails, and when I finally wanted to return the parsed content, I found that the return wasNone Can content but can print out, very puzzling. Then I found a solution on the Internet before I figured it out Record it here.

Original text:https:///article/

The original test code is below:

def print_info(msg, indent=0):
 if indent == 0:
  for header in ['From', 'To', 'Subject']:
   value = (header, '')
   if value:
    if header == 'Subject':
     value = decode_str(value)
    else:
     hdr, addr = parseaddr(value)
     name = decode_str(hdr)
     value = u'%s <%s>' % (name, addr)
   print('%s%s: %s' % (' ' * indent, header, value))
 
 if msg.is_multipart():
  parts = msg.get_payload()
  for n, part in enumerate(parts):
   print('%spart %s' % (' ' * indent, n))
   print('%s--------------------' % (' ' * indent))
   print_info(part, indent + 1) # There's no return here #
 else:
  content_type = msg.get_content_type()
  if content_type=='text/plain' or content_type=='text/html':
   content = msg.get_payload(decode=True)
   charset = guess_charset(msg)
   if charset:
    content = (charset)
   print('%sText: %s' % (' ' * indent, content))
   return content
  else:
   print('%sAttachment: %s' % (' ' * indent, content_type))

The content returned in this way is None.

The modifications are as follows:

def print_info(msg, indent=0):
 if indent == 0:
  for header in ['From', 'To', 'Subject']:
   value = (header, '')
   if value:
    if header == 'Subject':
     value = decode_str(value)
    else:
     hdr, addr = parseaddr(value)
     name = decode_str(hdr)
     value = u'%s <%s>' % (name, addr)
   print('%s%s: %s' % (' ' * indent, header, value))
 
 if msg.is_multipart():
  parts = msg.get_payload()
  for n, part in enumerate(parts):
   print('%spart %s' % (' ' * indent, n))
   print('%s--------------------' % (' ' * indent))
   return print_info(part, indent + 1) Returns the function itself directly on a recursive call
 else:
  content_type = msg.get_content_type()
  if content_type=='text/plain' or content_type=='text/html':
   content = msg.get_payload(decode=True)
   charset = guess_charset(msg)
   if charset:
    content = (charset)
   print('%sText: %s' % (' ' * indent, content))
   return content
  else:
   print('%sAttachment: %s' % (' ' * indent, content_type))

The difference is that the recursive call returns the function itself together. This allows the final recursive result to be returned bit by bit, which can be solved by returning the result of theNoneof the problem.

summarize

to this article on the python recursive call to return None of the problems and solutions to this article, more related python recursive return None content please search my previous articles or continue to browse the following related articles I hope you will support me more in the future!