• <tfoot id='F6JVn'></tfoot>

  • <legend id='F6JVn'><style id='F6JVn'><dir id='F6JVn'><q id='F6JVn'></q></dir></style></legend>

    <small id='F6JVn'></small><noframes id='F6JVn'>

          <bdo id='F6JVn'></bdo><ul id='F6JVn'></ul>
        <i id='F6JVn'><tr id='F6JVn'><dt id='F6JVn'><q id='F6JVn'><span id='F6JVn'><b id='F6JVn'><form id='F6JVn'><ins id='F6JVn'></ins><ul id='F6JVn'></ul><sub id='F6JVn'></sub></form><legend id='F6JVn'></legend><bdo id='F6JVn'><pre id='F6JVn'><center id='F6JVn'></center></pre></bdo></b><th id='F6JVn'></th></span></q></dt></tr></i><div id='F6JVn'><tfoot id='F6JVn'></tfoot><dl id='F6JVn'><fieldset id='F6JVn'></fieldset></dl></div>

        if 语句过多

        Too many if statements(if 语句过多)

          <bdo id='GTzoB'></bdo><ul id='GTzoB'></ul>
        • <small id='GTzoB'></small><noframes id='GTzoB'>

            <i id='GTzoB'><tr id='GTzoB'><dt id='GTzoB'><q id='GTzoB'><span id='GTzoB'><b id='GTzoB'><form id='GTzoB'><ins id='GTzoB'></ins><ul id='GTzoB'></ul><sub id='GTzoB'></sub></form><legend id='GTzoB'></legend><bdo id='GTzoB'><pre id='GTzoB'><center id='GTzoB'></center></pre></bdo></b><th id='GTzoB'></th></span></q></dt></tr></i><div id='GTzoB'><tfoot id='GTzoB'></tfoot><dl id='GTzoB'><fieldset id='GTzoB'></fieldset></dl></div>
            <legend id='GTzoB'><style id='GTzoB'><dir id='GTzoB'><q id='GTzoB'></q></dir></style></legend>

              <tbody id='GTzoB'></tbody>
                  <tfoot id='GTzoB'></tfoot>
                  本文介绍了if 语句过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些话题要讨论.我有一段代码,包含 24 个 ifs/elifs.Operation 是我自己的类,表示类似于 Enum 的功能.
                  这是一段代码:

                  I have some topic to discuss. I have a fragment of code with 24 ifs/elifs. Operation is my own class that represents functionality similar to Enum.
                  Here is a fragment of code:

                  if operation == Operation.START:
                      strategy = strategy_objects.StartObject()
                  elif operation == Operation.STOP:
                      strategy = strategy_objects.StopObject()
                  elif operation == Operation.STATUS:
                      strategy = strategy_objects.StatusObject()
                  (...)
                  

                  从可读性的角度来看,我有一些顾虑.将其更改为 24 个类并使用 polymorphism 会更好吗?我不相信它会使我的代码可维护......一方面这些 if 非常清楚,应该不难理解,另一方面有太多 如果s.

                  I have concerns from readability point of view. Is is better to change it into 24 classes and use polymorphism? I am not convinced that it will make my code maintainable... From one hand those ifs are pretty clear and it shouldn't be hard to follow, on the other hand there are too many ifs.

                  我的问题相当笼统,但是我正在用 Python 编写代码,所以我不能使用像 开关.

                  My question is rather general, however I'm writing code in Python so I cannot use constructions like switch.

                  你怎么看?

                  更新:

                  重要的一点是 StartObject()StopObject()StatusObject() 是构造函数,我想将一个对象分配给策略参考.

                  One important thing is that StartObject(), StopObject() and StatusObject() are constructors and I wanted to assign an object to strategy reference.

                  推荐答案

                  你可以使用字典.字典存储引用,这意味着函数完全可以使用,如下所示:

                  You could possibly use a dictionary. Dictionaries store references, which means functions are perfectly viable to use, like so:

                  operationFuncs = {
                      Operation.START: strategy_objects.StartObject
                      Operation.STOP: strategy_objects.StopObject
                      Operation.STATUS: strategy_objects.StatusObject
                      (...)                  
                  }
                  

                  最好有一个默认操作以防万一,因此当您运行它时,请使用 try except 并处理异常(即相当于您的 else 子句)

                  It's good to have a default operation just in case, so when you run it use a try except and handle the exception (ie. the equivalent of your else clause)

                  try:
                      strategy = operationFuncs[operation]()
                  except KeyError:
                      strategy = strategy_objects.DefaultObject()
                  

                  或者使用字典的 get 方法,如果没有找到您提供的键,您可以使用该方法指定默认值.

                  Alternatively use a dictionary's get method, which allows you to specify a default if the key you provide isn't found.

                  strategy = operationFuncs.get(operation(), DefaultObject())
                  

                  请注意,将它们存储在字典中时不包括括号,只在调用字典时使用它们.这也要求 Operation.START 是可散列的,但应该是这种情况,因为您将其描述为类似于 ENUM 的类.

                  Note that you don't include the parentheses when storing them in the dictionary, you just use them when calling your dictionary. Also this requires that Operation.START be hashable, but that should be the case since you described it as a class similar to an ENUM.

                  这篇关于if 语句过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <tfoot id='2nm0q'></tfoot>

                    <legend id='2nm0q'><style id='2nm0q'><dir id='2nm0q'><q id='2nm0q'></q></dir></style></legend>
                    1. <i id='2nm0q'><tr id='2nm0q'><dt id='2nm0q'><q id='2nm0q'><span id='2nm0q'><b id='2nm0q'><form id='2nm0q'><ins id='2nm0q'></ins><ul id='2nm0q'></ul><sub id='2nm0q'></sub></form><legend id='2nm0q'></legend><bdo id='2nm0q'><pre id='2nm0q'><center id='2nm0q'></center></pre></bdo></b><th id='2nm0q'></th></span></q></dt></tr></i><div id='2nm0q'><tfoot id='2nm0q'></tfoot><dl id='2nm0q'><fieldset id='2nm0q'></fieldset></dl></div>

                            <bdo id='2nm0q'></bdo><ul id='2nm0q'></ul>
                          • <small id='2nm0q'></small><noframes id='2nm0q'>

                              <tbody id='2nm0q'></tbody>