在python中迭代函数参数
我如何迭代参数以打印出函数中每个参数的这些行,而不是键入它们在python中迭代函数参数,python,function,arguments,iteration,Python,Function,Arguments,Iteration,我如何迭代参数以打印出函数中每个参数的这些行,而不是键入它们 def validate_user(surname, username, passwd, password, errors): errors = [] surname = surname.strip() # no digits if not surname: errors.append('Surname may not be empty, please enter surname')
def validate_user(surname, username, passwd, password, errors):
errors = []
surname = surname.strip() # no digits
if not surname:
errors.append('Surname may not be empty, please enter surname')
elif len(surname) > 20:
errors.append('Please shorten surname to atmost 20 characters')
username = username.strip()
if not username:
errors.append('Username may not be empty, please enter a username')
elif len(surname) > 20:
errors.append('Please shorten username to atmost 20 characters')
您可以通过将每个参数放入函数内的列表来迭代它们:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
您可以通过将每个参数放入函数内的列表来迭代它们:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
您可以通过将每个参数放入函数内的列表来迭代它们:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
您可以通过将每个参数放入函数内的列表来迭代它们:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
列出这些参数:
def validate_user(surname, username, passwd, password, errors):
for n in [surname, username]:
n = n.strip()
# Append the following printed strings to a list if you want to return them..
if not n:
print("{} is not valid, enter a valid name..".format(n))
if len(n) > 20:
print("{} is too long, please shorten.".format(n))
我应该注意,这实际上只对简单的姓氏或用户名验证有效。列出这些参数:
def validate_user(surname, username, passwd, password, errors):
for n in [surname, username]:
n = n.strip()
# Append the following printed strings to a list if you want to return them..
if not n:
print("{} is not valid, enter a valid name..".format(n))
if len(n) > 20:
print("{} is too long, please shorten.".format(n))
我应该注意,这实际上只对简单的姓氏或用户名验证有效。列出这些参数:
def validate_user(surname, username, passwd, password, errors):
for n in [surname, username]:
n = n.strip()
# Append the following printed strings to a list if you want to return them..
if not n:
print("{} is not valid, enter a valid name..".format(n))
if len(n) > 20:
print("{} is too long, please shorten.".format(n))
我应该注意,这实际上只对简单的姓氏或用户名验证有效。列出这些参数:
def validate_user(surname, username, passwd, password, errors):
for n in [surname, username]:
n = n.strip()
# Append the following printed strings to a list if you want to return them..
if not n:
print("{} is not valid, enter a valid name..".format(n))
if len(n) > 20:
print("{} is too long, please shorten.".format(n))
我应该注意,这实际上只对简单的姓氏或用户名验证有效。您真正想要的是本地人
def f(a, b, c):
for k, v in locals().items():
print k, v
或者类似的东西。你真正想要的是当地人
def f(a, b, c):
for k, v in locals().items():
print k, v
或者类似的东西。你真正想要的是当地人
def f(a, b, c):
for k, v in locals().items():
print k, v
或者类似的东西。你真正想要的是当地人
def f(a, b, c):
for k, v in locals().items():
print k, v
或者类似的东西。除了所有答案之外,您还可以使用 编辑: 在不使用函数名称的情况下:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
该函数可能如下所示:
def validate_user(surname, username, passwd, password, errors):
errors = []
for arg in inspect.getargspec(validate_user).args[:-1]:
value = eval(arg)
if not value:
errors.append("{0} may not be empty, please enter a {1}.".format(arg.capitalize(), arg))
elif len(value) > 20:
errors.append("Please shorten {0} to atmost 20 characters (|{1}|>20)".format(arg,value))
return errors
>>> validate_user("foo","","mysuperlongpasswordwhichissecure","",[])
['Username may not be empty, please enter a username.', 'Please shorten passwd to atmost 20 characters (|mysuperlongpasswordwhichissecure|>20)', 'Password may not be empty, please enter a password.']
除了所有答案,您还可以使用 编辑: 在不使用函数名称的情况下:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
该函数可能如下所示:
def validate_user(surname, username, passwd, password, errors):
errors = []
for arg in inspect.getargspec(validate_user).args[:-1]:
value = eval(arg)
if not value:
errors.append("{0} may not be empty, please enter a {1}.".format(arg.capitalize(), arg))
elif len(value) > 20:
errors.append("Please shorten {0} to atmost 20 characters (|{1}|>20)".format(arg,value))
return errors
>>> validate_user("foo","","mysuperlongpasswordwhichissecure","",[])
['Username may not be empty, please enter a username.', 'Please shorten passwd to atmost 20 characters (|mysuperlongpasswordwhichissecure|>20)', 'Password may not be empty, please enter a password.']
除了所有答案,您还可以使用 编辑: 在不使用函数名称的情况下:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
该函数可能如下所示:
def validate_user(surname, username, passwd, password, errors):
errors = []
for arg in inspect.getargspec(validate_user).args[:-1]:
value = eval(arg)
if not value:
errors.append("{0} may not be empty, please enter a {1}.".format(arg.capitalize(), arg))
elif len(value) > 20:
errors.append("Please shorten {0} to atmost 20 characters (|{1}|>20)".format(arg,value))
return errors
>>> validate_user("foo","","mysuperlongpasswordwhichissecure","",[])
['Username may not be empty, please enter a username.', 'Please shorten passwd to atmost 20 characters (|mysuperlongpasswordwhichissecure|>20)', 'Password may not be empty, please enter a password.']
除了所有答案,您还可以使用 编辑: 在不使用函数名称的情况下:
def validate(a,b,c):
for item in [a,b,c]:
print item
a=1
b=2
c=3
validate(a,b,c)
>>> def f(a,e,d,h):
... print inspect.getargvalues(inspect.currentframe()).args
...
>>> f(1,2,3,4)
['a', 'e', 'd', 'h']
该函数可能如下所示:
def validate_user(surname, username, passwd, password, errors):
errors = []
for arg in inspect.getargspec(validate_user).args[:-1]:
value = eval(arg)
if not value:
errors.append("{0} may not be empty, please enter a {1}.".format(arg.capitalize(), arg))
elif len(value) > 20:
errors.append("Please shorten {0} to atmost 20 characters (|{1}|>20)".format(arg,value))
return errors
>>> validate_user("foo","","mysuperlongpasswordwhichissecure","",[])
['Username may not be empty, please enter a username.', 'Please shorten passwd to atmost 20 characters (|mysuperlongpasswordwhichissecure|>20)', 'Password may not be empty, please enter a password.']
您可以使用
*args
和**kwargs
获得类似的行为,但它可能更干净/更清晰,除非您调用函数时不知道它的参数是什么。您可以使用*args
和**kwargs
获得类似的行为,但它可能更干净/更清晰,除非您正在调用该函数,否则您不知道它的参数是什么。您可能会在*args
和**kwargs
中获得类似的行为,并且它可能会更干净一些,除非你调用函数,否则你不知道它的参数是什么。你可能会在*args
和**kwargs
中得到类似的行为,而且它可能会更干净一些,除非你调用函数,那么你不知道它的参数是什么。