如果 Python 为您构建 dict,它是否算数?
studio = 'Warner Brothers'
movie = ''
validation = ['studio', 'movie']
validation_errors = []
for item in validation:
if not locals()[item]:
validation_errors.append(item)
但是,自己构建字典是更好的主意。
studio = 'Warner Brothers'
movie = ''
validation = dict(studio=studio, movie=movie)
validation_errors = []
for var, value in validation.items():
if not value:
validation_errors.append(var)
如果你想用它做一个功能:
def validate(**vars):
errors = []
for var, value in vars.items():
if not value:
errors.append(var)
return errors
-->studio = 'Warner Brothers'
-->movie = ''
-->print validate(studio=studio, movie=movie)
['movie']