我必须携带一个“灰尘”;2.5(具体细节为2.4.3)。
看来,从2.5开始便引进了家禽。 对于那些不熟悉的人来说,位于Avre >的tern客;=2.5看上去就是这样:
def do_ternary(flag):
return "foo" if flag else "bar"
我很想知道一些解决办法,以便在早期版本的沙迦中效仿。 我可以肯定地说,如果是......,那么我会想到的是,我会毫不犹豫地提出某种生产水平的法典。
感谢帮助!
我必须携带一个“灰尘”;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
你们对此有何看法? 我认为,它非常干净,容易阅读。
< 确实:
(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指出,这种做法可能与房主略有不同,因为真实和错误的价值观都将得到评估,可能产生副作用。
这样做的典型trick计是:
test and true_value or false_value
http://code>and和。 work:
x or y -> if x is false, then y, else x
x and y -> if x is false, then x, else y
This means that we get the roughly the same result - so long as true_value
evaluates to True
- so, for example, the following would not work:
flag and [] or "bar"
As []
evaluates to False
.
我现在仍然认为,这比仅仅使用一种假体,不如说是可读的,因为除非你熟悉,否则情况不明确。
因此,我建议使用:
if test:
return true_value
else:
return false_value
(以转让方式或根据需要以任何方式进行返回)。
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ]="...