社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

handras

handras 最近回复了
7 年前
回复了 handras 创建的主题 » 如何在函数python中调用类

你想把学生名单上的所有学生都带走。所以在for循环中使用它。另外,您在方法中正确地封装了通过/失败标准的逻辑,因此请使用它。

我想这是你想要的代码:

def failed(list_of_students):
   failed_students = []
   for student in list_of_students:  
       if not student.passes():  
           failed_students.append(student.name)  
   return failed_students

更高级的方法是使用列表理解:

def failed(list_of_students):
    return [student for student in list_of_students if not student.passes()]

它更具Pythic,但对于C语言或Java背景的初学者来说,我更难理解。