I read several articles about alternatives to switch statements in Python. Mostly using dicts instead of a lot of if and elif. However, in reality, no one answers the question: do you have the best performance or efficiency? I read a few arguments that if elifs would need to test each statement and become ineffective with many ifs and elif. However, using dicts turns around this, but you need to create new modules for calling, which in any case cancel the performance gain. The only difference is that it is ultimately readable.
Can anyone comment on this, is there any difference in the long run? Does anyone regularly use an alternative? The only reason I ask is that I will end up with 30-40 elif / if and possibly more in the future. Any input is appreciated. Thanks.
dict , , , dict O (1), ( ;-). " ", , , , dict, , , - , , .
dict
, timeit . : , , , - - , , !)
timeit
, , , , , SO ! -)
, . , , . ( ), 30-40 elif - , - . , , , - .
/ - " ".
- timeit, python.
, , switch. , . , , .
switch
( - ) , .
, , dict Python. switch, Python ( ), .
dicts Python. CPython dicts - , -. - O (1), elif, O (n). (30-40, , , ). , , dicts .
, . ( Python-!), , . , , , .
, if, , . , , if . , , , . .
if
(. ). , . : , .
Source: https://habr.com/ru/post/1722194/More articles:Creating a Cocoa Application Using C or Perl (NOT Objective-C) - cModel-View-Presenter in ASP.NET with ListView and Repeater - c #Can I use UDK for non-game projects? - editorCodeIgniter PHP Exceptions - phpHow can two C # applications send messages over a Wi-Fi network? - c #Как узнать C-указатели? - cBest F # IDE for Mac - ideiPhone facebook connects FQL query to get user friends - iphone.NET: System.IO.Path - .netDelete an object by key without first extracting it in the application engine (using JDO) - javaAll Articles