Python函数如何先return一个信息,再继续执行后续代码

Python语言,函数A调用函数B,B先return一个信息给A,接着调用函数C,请问如何实现

def C():
    print("C")
    
def myfunc():
    yield 123
    C()
    yield 456

def A():
    gen = myfunc()
    print(gen.__next__())
    print("然后继续执行")
    gen.__next__()
    
A()


def functionA():
    # 调用函数B并接收返回的信息
    info = functionB()
    
    # 在这里可以使用返回的信息进行处理
    print("Received info from functionB:", info)
    
    # 继续执行后续代码
    functionC()

def functionB():
    # 返回一个信息给调用它的函数
    return "Hello from functionB!"

def functionC():
    print("This is functionC.")

# 调用函数A
functionA()

输出

Received info from functionB: Hello from functionB!
This is functionC.


  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7710559
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:Python 对A表和B表比对两组数据
  • 除此之外, 这篇博客: Python爬虫抓取B站热榜中的 实现代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • # -*- coding:utf-8  -*-
    import re
    from bs4 import BeautifulSoup
    import urllib
    import pandas as pd
    
    class Spider():
    	'''
            Description:
                Spider program to crawl data from bilibili.com hot search rank list
            Attributes:
                None
        '''
        def __init__(self):
            self.url = 'https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3'
    		
    		# regular matching pattern
            self.pattern = re.compile(r'<a class="title" href="(.*?)" target="_blank">(.*?)</a>')
    
            self.headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"}
    	
    	'''
            Description:
                crawl page from the given URL
            Args:
                url: the URL of page need to get
            Returns:
                page of the given URL
        '''
        def crawl(self):
            self.request = urllib.request.Request(headers = self.headers, url = self.url)
            self.response = urllib.request.urlopen(self.request)
            page = self.response.read().decode('utf-8')
    
            return page
    	
    	'''
            Description:
                extract data from the given page by bs4 and re library, return the list of data
            Args:
                None
            Returns:
                list of data extract from given page
        '''
        def extract(self):
            page = self.crawl()
            beautifulSoup = BeautifulSoup(page, 'html.parser')
    
            results = []
    
            for frame in beautifulSoup.find_all('div', class_ = 'content'):
                frame = str(frame)
                result = re.findall(self.pattern, frame)[0]
                results.append(result)
    
            return results
    
    if __name__ == "__main__":
        spider = Spider()
        results = spider.extract()
        print(pd.DataFrame(results))
    
  • 您还可以看一下 陈滨老师的Python零基础入门课程中的 安装Python环境b小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    在函数B中,可以通过input()函数让用户输入信息,并保存在一个变量中。然后再返回这个变量给函数A。具体实现可以参考如下代码:

    def functionB():
        user_input = input("Please enter information:")
        return user_input
    
    def functionA():
        result_from_B = functionB()
        #continue to execute the code in functionA
        return result_from_B