有没有更好的方法来避免冗余代码?在这种情况下,如果storage
是 0 那么我不想将它传递给函数并保持代码整洁,但我不得不做一个 if-else。
if storage == 0:
response_lc = asg_client.create_launch_configuration(
ImageId=ami,
InstanceType=instance_type,
KeyName=keypair
)
else:
response_lc = asg_client.create_launch_configuration(
VolumeSize=storage,
ImageId=ami,
InstanceType=instance_type,
KeyName=keypair
)
如果没有出路,你可以这样做:
kwargs = {
"ImageId": ami,
"InstanceType": instance_type,
"KeyName": keypair
}
if storage:
kwargs["VolumeSize"] = storage
response_lc = asg_client.create_launch_configuration(**kwargs)
但我会建议你深入研究该asg_client.create_launch_configuration
方法,看看是否在所有情况下都可以传递所有四个参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句