Dictionary or If statements, Jython

前端 未结 5 615
清酒与你
清酒与你 2021-01-06 06:04

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 stat

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-06 06:12

    Your use of the dictionary is not quite correct. In your implementation, all methods will be called and all the useless one discarded. What is usually done is more something like:

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

    And that way is facter and more extensible if you have a large (or variable) number of items.

提交回复
热议问题