Re: [討論] Python 3.10將加入Switch-Case語句
我個人是很討厭很多if-else, 或是switch case.
並不是說不好, 只是很容易出現有些section是code, 有些是function.
案子急一點, 重覆的code就會很多.
幾百個if-else/switch-case就有機會變成上萬行的code. 這個就很阿雜了.
就之前數字區間的code, 我是會往這個方向走
import operator
def over_100m(s):
print('over 100m')
def ten_to_100m(s):
print('10 - 100m')
def zero_to_ten(s):
print('0 - 10')
def is_zero(s):
print('0')
def is_negative(s):
print('< 0')
map = {(operator.ge, 100000000): over_100m,
(operator.ge, 10): ten_to_100m,
(operator.gt, 0): zero_to_ten,
(operator.eq, 0): is_zero,
(operator.lt, 0): is_negative}
eigen_spectrum = [120000000, 1000, 10, 8, 0, -10]
for s in eigen_spectrum:
for c in map:
if c[0](s, c[1]):
map[c](s)
break
這樣至少可以強迫分工的時候, 有一個列表可以作維護.
不過coding style這個真的見人見智, 我是不覺得有絕對好壞之分.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.144.241 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1616854807.A.2F3.html
推
03/27 22:22,
4年前
, 1F
03/27 22:22, 1F
→
03/27 22:22,
4年前
, 2F
03/27 22:22, 2F
→
03/27 23:07,
4年前
, 3F
03/27 23:07, 3F
→
03/27 23:08,
4年前
, 4F
03/27 23:08, 4F
→
03/27 23:08,
4年前
, 5F
03/27 23:08, 5F
→
03/27 23:24,
4年前
, 6F
03/27 23:24, 6F
→
03/27 23:46,
4年前
, 7F
03/27 23:46, 7F
→
04/04 06:02, , 8F
04/04 06:02, 8F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 6 篇):
Soft_Job 近期熱門文章
18
31
26
104
PTT職涯區 即時熱門文章
25
39