• <small id='qfMIF'></small><noframes id='qfMIF'>

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

    <tfoot id='qfMIF'></tfoot>

        字典或 If 语句,Jython

        Dictionary or If statements, Jython(字典或 If 语句,Jython)
        <tfoot id='oQD22'></tfoot>
          <i id='oQD22'><tr id='oQD22'><dt id='oQD22'><q id='oQD22'><span id='oQD22'><b id='oQD22'><form id='oQD22'><ins id='oQD22'></ins><ul id='oQD22'></ul><sub id='oQD22'></sub></form><legend id='oQD22'></legend><bdo id='oQD22'><pre id='oQD22'><center id='oQD22'></center></pre></bdo></b><th id='oQD22'></th></span></q></dt></tr></i><div id='oQD22'><tfoot id='oQD22'></tfoot><dl id='oQD22'><fieldset id='oQD22'></fieldset></dl></div>

        1. <small id='oQD22'></small><noframes id='oQD22'>

                <tbody id='oQD22'></tbody>

              • <bdo id='oQD22'></bdo><ul id='oQD22'></ul>
                <legend id='oQD22'><style id='oQD22'><dir id='oQD22'><q id='oQD22'></q></dir></style></legend>
                • 本文介绍了字典或 If 语句,Jython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am writing a script at the moment that will grab certain information from HTML using dom4j.

                  Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below:

                  if type == 'extractTitle':
                      extractTitle(dom)
                  if type == 'extractMetaTags':
                      extractMetaTags(dom)
                  

                  I will be adding more depending on what information I want to extract from the HTML and thought about taking the dictionary approach which I found elsewhere on this site, example below:

                  {
                      'extractTitle':    extractTitle,
                      'extractMetaTags': extractMetaTags
                  }[type](dom)
                  

                  I know that each time I run the script the dictionary will be built, but at the same time if I were to use the if statements the script would have to check through all of them until it hits the correct one. What I am really wondering, which one performs better or is generally better practice to use?

                  Update: @Brian - Thanks for the great reply. I have a question, if any of the extract methods require more than one object, e.g.

                  handle_extractTag(self, dom, anotherObject)
                  # Do something
                  

                  How would you make the appropriate changes to the handle method to implemented this? Hope you know what I mean :)

                  Cheers

                  解决方案

                  To avoid specifying the tag and handler in the dict, you could just use a handler class with methods named to match the type. Eg

                  class  MyHandler(object):
                      def handle_extractTitle(self, dom):
                          # do something
                  
                      def handle_extractMetaTags(self, dom):
                          # do something
                  
                      def handle(self, type, dom):
                          func = getattr(self, 'handle_%s' % type, None)
                          if func is None:
                              raise Exception("No handler for type %r" % type)
                          return func(dom)
                  

                  Usage:

                   handler = MyHandler()
                   handler.handle('extractTitle', dom)
                  

                  Update:

                  When you have multiple arguments, just change the handle function to take those arguments and pass them through to the function. If you want to make it more generic (so you don't have to change both the handler functions and the handle method when you change the argument signature), you can use the *args and **kwargs syntax to pass through all received arguments. The handle method then becomes:

                  def handle(self, type, *args, **kwargs):
                      func = getattr(self, 'handle_%s' % type, None)
                      if func is None:
                          raise Exception("No handler for type %r" % type)
                      return func(*args, **kwargs)
                  

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

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

                  相关文档推荐

                  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 与否?)
                • <i id='5mUAr'><tr id='5mUAr'><dt id='5mUAr'><q id='5mUAr'><span id='5mUAr'><b id='5mUAr'><form id='5mUAr'><ins id='5mUAr'></ins><ul id='5mUAr'></ul><sub id='5mUAr'></sub></form><legend id='5mUAr'></legend><bdo id='5mUAr'><pre id='5mUAr'><center id='5mUAr'></center></pre></bdo></b><th id='5mUAr'></th></span></q></dt></tr></i><div id='5mUAr'><tfoot id='5mUAr'></tfoot><dl id='5mUAr'><fieldset id='5mUAr'></fieldset></dl></div>
                  <legend id='5mUAr'><style id='5mUAr'><dir id='5mUAr'><q id='5mUAr'></q></dir></style></legend>

                        <tbody id='5mUAr'></tbody>
                    • <tfoot id='5mUAr'></tfoot>

                      <small id='5mUAr'></small><noframes id='5mUAr'>

                          • <bdo id='5mUAr'></bdo><ul id='5mUAr'></ul>