关于“To be, or not to be”的一个小话题

 To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep;
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to, 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;
For who would bear the whips and scorns of time,
The oppressor's wrong, the proud man's contumely,
The pangs of despised love, the law's delay,
The insolence of office and the spurns
That patient merit of the unworthy takes,
When he himself might his quietus make
With a bare bodkin? who would fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscover'd country from whose bourn
No traveller returns, puzzles the will
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience does make cowards of us all;
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pitch and moment
With this regard their currents turn awry,
And lose the name of action.--Soft you now!
The fair Ophelia! Nymph, in thy orisons
Be all my sins remember'd".

上边这段,出自莎士比亚出《哈姆雷特》第三幕第一场,哈姆雷特王子独白,其中第一句“To be, or not to be”是脍炙人口的名句。名句自然有名译,当然名译也会有很多版本,比如—— 

朱生豪:生存还是毁灭,这是一个值得考虑的问题; 
梁实秋:死后还是存在,还是不存在,——这是问题; 
曹未风:生存还是不生存:就是这个问题: 
孙大雨:是生存还是消亡,问题的所在; 
林同济:存在,还是毁灭,就这问题了。 
方平:活着好,还是死了好,这是个难题啊: 
卞之琳:活下去还是不活:这是问题。 
王佐良:生或死,这就是问题所在。 
许渊冲:死还是不死,这是个问题。 
裘克安:活着,还是不活了,问题就在这里: 
陈国华:是生,还是死,问题就在这里: 

 

又扯远了哈,回来说正题。“To be, or not to be”的本意,以及如何翻译,都是一个选择的问题,在结构化程序中“选择”是一个非常重要的部分,而对于程序设计来说,“选择”以及其所蕴含的逻辑操作模式,是自动化处理的核心。下面就来看看个python如何在一行中实现选择分支。

先看一种好理解的方式。首先要注意的是,由于语句会影响当前空间内的变量,我们必须要保证满足条件以后再执行语句,而不是选择不同语句执行的结果。所以下面的方式是一种可行方案:

>>>(['a' for x in [1] if 1==1],['b' for x in [1] if 1==2])

其中1==1和1==2就是if中的判断部分,'a'和'b'就是满足条件时执行的语句。这样,无论你有多少个判断,都可以一个个排列出来去执行。但是这种方式虽然结构很容易理解,但是看上去臃肿的很,而且每次都判断,效率也不高。如果能像c语言的问号表达式一样,才称得上是漂亮的解决方案。不过这里我们先把这个美丽的设想放下,从另一个角度说起。

不像大多数支持结构化程序设计思想的语言,python里没有switch-case结构。如果你对此耿耿于怀的话,相信下面这段手册中关于if语句的语法结构定义一定不陌生:

if_stmt ::= "if" expression ":" suite
    ( "elif" expression ":" suite )*
    ["else" ":" suite

确实,多次使用elif可以模拟出switch-case结构,但是如果你在查阅资料的过程中到这里一游过:https://docs.python.org/2/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python ,你对本文开头提出的问题一定是胸有成竹。看名字就知道,前面的链接是python对于不设计switch-case语句的官方解释。里面租了这样的例子:

 

functions = {'a': function_1,
             'b': function_2,
             'c': self.method_1, ...}

func = functions[value]
func()

 

答案很明显了吧?使用lisi、dict等方式,将分支中的语句包含在子函数定义中,再根据判断变量的值来自动选择使用哪个函数,这样就可以将if甚至switch-case语句完全函数化。看下面的例子:

>>> sel=[lambda x:(x.insert(1,(x.pop(1)*3+1)),x[1])[1],lambda x:(x.insert(1,(x.pop(1)/2)),x[1])[1]]
>>> sel[(3%2)==0]([0,3])
10
>>> sel[(2%2)==0]([0,2])
1
>>>

这里,我们先将连个不同的函数放在名为sel的list中sel[0]的作用是返回输入list第二个参数乘3加1的值,sel[1]的作用是返回输入list第二个参数的值的一半。“(2%2)==0”这个结构是为了对分支条件进行判断,以决定用哪个函数。由于:

>>> ('a'=='a')==1
True
>>> ('a'=='b')==0
True

所以我们可以使用这种简单的方式将逻辑值True、False当作1、0来使用。那么有两个分支的if语句就可以变成以下模式:

[lambda varList:'false statement',lambda varList:'false statement']['test statement'](realVarList)


现在,我们已经解决了顺序结构、选择结构的编写方法,下一次就轮到最令人纠结的循环结构了。


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值