【发布时间】:2012-10-18 14:55:19
【问题描述】:
如果您将模式定义为一种巧妙的技巧,可以帮助您以一种优雅且可能更易读的方式解决编程问题[1]。使用continue 语句的设计模式是什么(如果要避免深度嵌套的if 语句)?
for item in items:
if is_for_sale(item):
cost=compute_cost(item)
if cost<=wallet.money:
buy(item)
for item in items:
if not is_for_sale(item):
continue
cost = compute_cost(item)
if cost > wallet.money:
continue
buy(item)
【问题讨论】:
-
设计模式不是实现模式。所以不确定你在问什么。
-
continue在其他语言中是一个非常常见的功能——我认为它在C中也称为continue,在fortran 中称为cycle。我在 python 中并没有真正使用它(因为我还没有发现非常需要) -
@mgilson -- 这就是我问这个问题的原因,似乎实际使用它的情况并不多(或者甚至应该使用它的地方)......跨度>
标签: python design-patterns refactoring