elif文を書くより効率的な方法があるかどうかを試してみます。クラスが呼び出されるパラメータの数に基づいてURLを生成するためのAPIを記述する。例についてはPythonで大規模なelif文を縮小する必要があります
:
def Cars(self, model=null, color=null, miles=null)
if model == null and color == null and miles ==null:
url = cars/
elif model != null and color == null and miles ==null:
url = cars/model=%s)% model
elif model != null and color != null and miles ==null:
url = cars/model=%s/color=%s)% model, color
else url = someting
return url
私は10個の以上のパラメータを持っており、すべての組み合わせで、多くのelif文を書きたくない...
文字列の前後に引用符がありません。 –
問題の投稿されたコードは無効ですPython。 –
不器用なコードを私たちの友人に許しましょう。 :P – Marcus