__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_no
,email
,那么如果找到该id的学生,那么只有roll& amp ;电子邮件将与姓名一起打印。但如果我没有通过任何论证,那么只会打印名称
答案 0 :(得分:2)
使用布尔值设置是否打印滚动和电子邮件的标志:
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作为密钥将用户存储在dict中:
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"
然后传递一个dict作为数据库:
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'
你可能最好使用dict来处理所有逻辑,但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")
或者如果返回False,请使用hasattr并执行任何操作:
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:....
答案 1 :(得分:0)
您可以使用**
表示法将可变数量的命名参数传递给您的函数。
如果参数为**args
,那么它将是函数定义中尚未定义的任何命名参数的字典,然后您可以检查roll_no
或email
是否为出现在args
中,如果是,请按照你的逻辑行事。
示例代码 -
>>> def func(**args):
... print(args)
... if 'abc' in args:
... print(args['abc'])
...
>>> func(hello=10)
{'hello': 10}
>>> func(hello=10,abc="Student")
{'hello': 10, 'abc': 'Student'}
Student
>>> def search_student(database=None, ID=None, **args):
... if 'roll_no' in args:
... print(args['roll_no'])
...
>>> search_student(roll_no=10)
10
答案 2 :(得分:0)
所以这不是答案,而是一篇帖子,因为@Rob要求进一步澄清
所以如果函数存在,请说func(database=None, ID=None, [optionals])
所以当你致电func(DATABASE, 1, roll_no, email)
时
ID为1&他/她的电子邮件和卷也将被打印
但如果我通过func(DATABASE, 1, roll_no, email, phone)
然后命名滚动电子邮件&将打印电话号码,但如果我只是给DATABASE& ID&没有选项,那么只应打印名称..如果没有找到该ID的记录,那么它不应该打印任何内容