gpt4 book ai didi

python - 如何在函数中传递 namedtuple 的可选参数

转载 作者:行者123 更新时间:2023-11-28 17:35:23 25 4
gpt4 key购买 nike

__author__ = 'mayukhsarkar'
import collections


def search_student(database=None, ID=None):
flag = False
try:
if ID is None or ID == 0:
raise TypeError
for each in database:
if each.id == ID:
print 'Student Name: ', each.name
flag = True
except TypeError:
print 'Provide the arguments properly'
finally:
return flag


studentDatabase = collections.namedtuple("student", "id name roll_no phone email")

DATABASE = [studentDatabase(1, 'Mayukh Sarkar', 9, '555-2312', 'mayukh2012@hotmail,com'),
studentDatabase(2, 'Alisha Sengupta', 7, '555-1345', 'alisha@gmail.com')]

if not search_student(DATABASE, 1):
print "Data not found"

在这个片段中,我想要函数 search_student 中的可变参数选项,这样如果我传递 roll_noemail 那么如果学生找到该 ID,然后只打印 roll & email 和姓名。但是如果我不传递任何参数,那么只会打印名称

最佳答案

使用 bool 值设置是否打印名册和邮件的标志:

def search_student(database=None, ID=None, roll=False, email=False):
if ID is None or ID == 0:
print 'Provide the arguments properly'
return False
for each in database:
if each.id == ID:
print 'Student Name: {}'.format(each.name)
if roll:
print 'Stutent Roll: {}'.format(each.roll_no)
if email:
print("Student email: {}".format(each.email))
break
else:
return "User does not exist in database"


studentDatabase = collections.namedtuple("student", "id name roll_no phone email")

DATABASE = [studentDatabase(1, 'Mayukh Sarkar', 9, '555-2312', 'mayukh2012@hotmail,com'),
studentDatabase(2, 'Alisha Sengupta', 7, '555-1345', 'alisha@gmail.com')]

输出:

In [9]: search_student(DATABASE, 1, email=True,roll=True)
Student Name: Mayukh Sarkar
Stutent Roll: 9
Student email: mayukh2012@hotmail,com

In [10]: search_student(DATABASE, 1,roll=True)
Student Name: Mayukh Sarkar
Stutent Roll: 9

In [11]: search_student(DATABASE, 1, email=True)
Student Name: Mayukh Sarkar
Student email: mayukh2012@hotmail,com

In [12]: search_student(DATABASE, 1)
Student Name: Mayukh Sarkar

In [13]: search_student(DATABASE, 5)
Out[13]: 'User does not exist in database'

我还会使用 id 作为键将用户存储在字典中:

def search_student(database=None, ID=None, roll=False, email=False):
if ID is None or ID == 0:
print 'Provide the arguments properly'
return False
get = database.get(ID)
if get is not None:
print 'Student Name: {}'.format(get.name)
if roll:
print 'Student Roll: {}'.format(get.roll_no)
if email:
print("Student email: {}".format(get.email))
else:
return "User does not exist in database"

然后传递一个字典作为数据库:

studentDatabase = collections.namedtuple("student", "id name roll_no phone email")

DATABASE = {1: studentDatabase(1, 'Mayukh Sarkar', 9, '555-2312', 'mayukh2012@hotmail,com'),
2: studentDatabase(2, 'Alisha Sengupta', 7, '555-1345', 'alisha@gmail.com')}

输出是一样的:

In [18]: search_student(DATABASE, 1, email=True,roll=True)Student Name: Mayukh Sarkar
Student Roll: 9
Student email: mayukh2012@hotmail,com

In [19]: search_student(DATABASE, 1,roll=True)
Student Name: Mayukh Sarkar
Student Roll: 9

In [20]: search_student(DATABASE, 1, email=True)
Student Name: Mayukh Sarkar
Student email: mayukh2012@hotmail,com

In [21]: search_student(DATABASE, 1)
Student Name: Mayukh Sarkar

In [22]: search_student(DATABASE, 5)
Out[22]: 'User does not exist in database'

您最好对所有逻辑使用字典,但 getattr 将与 **kwargs 一起使用:

import collections


def search_student(database=None, ID=None, **kwargs):
if ID is None or ID == 0:
print 'Provide the arguments properly'
return False
get = database.get(ID)
if get is not None:
print 'Student Name: {}'.format(get.name)
for k in kwargs:
print("Student {}: {}".format(k, getattr(get,k)))
else:
return "User does not exist in database"


studentDatabase = collections.namedtuple("student", "id name roll_no phone email")

DATABASE = {1: studentDatabase(1, 'Mayukh Sarkar', 9, '555-2312', 'mayukh2012@hotmail,com'),
2: studentDatabase(2, 'Alisha Sengupta', 7, '555-1345', 'alisha@gmail.com')}

search_student(DATABASE,1,roll_no=True,email=True)

我们需要在用户输入无效关键字/属性时捕获,有很多方法可以处理它,但一种简单的方法是捕获属性错误:

        try:
print("Student {}: {}".format(k, getattr(get, k)))
except AttributeError:
print("Informative message or whatever is suitable")

或者使用 hasattr 并在它返回 False 时做任何你想做的事:

 if get is not None:
print 'Student Name: {}'.format(get.name)
for k in kwargs:
if not hasattr(get,k):
continue
print("Student {}: {}".format(k, getattr(get, k)))

或者传递一个默认值给getattr:

if get is not None:
print 'Student Name: {}'.format(get.name)
for k in kwargs:
val = getattr(get, k,False)
if val:
print("Student {}: {}".format(k, val))
else:....

关于python - 如何在函数中传递 namedtuple 的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31089612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com