English 中文(简体)
达到<2.5>最低条件的最佳途径
原标题:Best way to do ternary conditionals in Python < 2.5

我必须携带一个“灰尘”;2.5(具体细节为2.4.3)。

看来,从2.5开始便引进了家禽。 对于那些不熟悉的人来说,位于Avre >的tern客;=2.5看上去就是这样:

def do_ternary(flag):
    return "foo" if flag else "bar"

我很想知道一些解决办法,以便在早期版本的沙迦中效仿。 我可以肯定地说,如果是......,那么我会想到的是,我会毫不犹豫地提出某种生产水平的法典。

感谢帮助!

最佳回答

实际上,我看着这个网络,发现似乎像真正的老化解决办法:

def _if(test):
    return lambda alternative: 
               lambda result: 
                   [delay(result), delay(alternative)][not not test]()

def delay(f):
    if callable(f): return f
    else: return lambda: f

>>> fact = lambda n: _if (n <= 1) (1) (lambda: n * fact(n-1))
>>> fact(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

你们对此有何看法? 我认为,它非常干净,容易阅读。

问题回答

<correct >, 一切照样 确实:

(condition and (yes_value,) or (no_value,))[0]

短期电路和解决<代码>yes_ Value/code>的问题本身都是不实的。 显然,如果你有理由避免这一udge子,那就是这样做的。

{True: yes_value, False: no_value}[bool(condition)]

或更可怕的是:

(no_value, yes_value)[condition]

页: 1 我们需要简短的限定,但你相信,“是——价值永远不会错”,你可以 tri:

 condition and yes_value or no_value

但只有在<代码>es_ Value/code>实际上保持不变时,才可能有效。 如果其中无一符合你的教义或需要,则只使用一个便携式的<代码>。

if condition:
    result = yes_value
else:
    result = no_value

共同的切trick是使用名单索引,因为<代码>False /True 转至0/。 必要时进行分类。 如果检验可能是虚假的或真实的,而不是ool的,那么首先确保检验的好做法是 b:

["bar", "foo"][bool(flag)]

将产生与贵方相同的产出。

Edit: Dougal指出,这种做法可能与房主略有不同,因为真实和错误的价值观都将得到评估,可能产生副作用。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...