提案:模拟C?:运算符的函数 [英] Proposal: function which simulates C ?: operator

查看:59
本文介绍了提案:模拟C?:运算符的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为应该在

内置模块中添加类似下面的功能:


def boolselect(condition,trueresult,falseresult):

如果条件:

返回trueresult

else:

返回falseresult


--- WITH


scalewidth = boolselect(self .__ vertical,_scalew,_scaleh)


a1 = 0

a4 = boolselect(self .__ vertical,self .__ width,self .__ height)

a2 =(a4 - scalewidth)// 2

a3 = a2 + scalewidth


self .__ meterwidth = self .__ meterrect [0] .Width()

self .__ meterheight = self .__ meterrect [0] .Height()

self .__ meterlength = boolselect(self .__ vertical,self .__ meterheight,self .__ meterwidth)


t = boolselect(self .__ range> = 18,-18 ,-12)

redzone = self .__ dBToOffset(-6)

yellowzone = self .__ dBToOffset(t)


- - 没有


如果自己.__垂直:

scalewidth = _scalew

else:

scalewidth = _scaleh


a1 = 0

如果自我.__垂直:

a4 =自我.__宽度

其他:

a4 =自我.__身高

a2 =( a4 - scalewidth)// 2

a3 = a2 + scalewidth

self .__ meterwidth = self .__ meterrect [0] .Width()

self .__ meterheight = self .__ meterrect [0] .Height()

if self .__ vertical:

self .__ meterlength = self .__ meterheight

else:

self .__ meterlength = self .__ meterwidth


if self .__ range> = 18:

t = -18

否则:

t = -12

redzone = self .__ dBToOffset(-6)

yellowzone = self .__ dBToOffset(t)

---


您怎么看?


PS:可能找到比boolselect更好的名字。

I think a function similar to the one below should be added to the
builtin module:

def boolselect(condition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult

--- WITH

scalewidth = boolselect(self.__vertical, _scalew, _scaleh)

a1 = 0
a4 = boolselect(self.__vertical, self.__width, self.__height)
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth

self.__meterwidth = self.__meterrect[0].Width()
self.__meterheight = self.__meterrect[0].Height()
self.__meterlength = boolselect(self.__vertical, self.__meterheight, self.__meterwidth)

t = boolselect(self.__range >= 18, -18, -12)
redzone = self.__dBToOffset(-6)
yellowzone = self.__dBToOffset(t)

--- WITHOUT

if self.__vertical:
scalewidth = _scalew
else:
scalewidth = _scaleh

a1 = 0
if self.__vertical:
a4 = self.__width
else:
a4 = self.__height
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth

self.__meterwidth = self.__meterrect[0].Width()
self.__meterheight = self.__meterrect[0].Height()
if self.__vertical:
self.__meterlength = self.__meterheight
else:
self.__meterlength = self.__meterwidth

if self.__range >= 18:
t = -18
else:
t = -12
redzone = self.__dBToOffset(-6)
yellowzone = self.__dBToOffset(t)

---

What do you think?

PS: maybe a better name than boolselect could be found.

推荐答案

Adal Chiriliuc< me@spammers.com>

(新闻:13 ***** ******************@smtp.myrealbox.com)写道:
Adal Chiriliuc <me@spammers.com>
(news:13***********************@smtp.myrealbox.com ) wrote:
我认为类似下面的功能应该是
添加到
内置模块:

def boolselect(condition,trueresult,falseresult):
如果条件:
返回trueresult
否则:
返回falseresult



scalewidth = boolselect(self .__ vertical,_scalew,_scaleh)

---没有
如果自我.__垂直:
scalewidth = _scalew
否则:
scalewidth = _scaleh
你怎么看?
I think a function similar to the one below should be
added to the
builtin module:

def boolselect(condition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult

--- WITH

scalewidth = boolselect(self.__vertical, _scalew, _scaleh)

--- WITHOUT

if self.__vertical:
scalewidth = _scalew
else:
scalewidth = _scaleh What do you think?



这里有一个漂亮的解决方法我想出了自己,但我相信其他人会把它用作

井:

scalewidth =(_ scaleh,_scalew)[__ self。垂直]


HTH


它不像if cond那么可读? true_expr:false_expr",但我认为

引入这样的语法远非需要而且相当复杂。



Here''s a nifty workaround I figured out myself but am sure others use it as
well:
scalewidth=(_scaleh,_scalew)[__self.vertical]

HTH

It''s not as readable as the "if cond ? true_expr : false_expr", but I think
introducing syntax like this is far from needed and pretty complicated.


在文章< 13 ** *********************@smtp.myrealbox.com>,

Adal Chiriliuc< me@spammers.com>写道:
In article <13***********************@smtp.myrealbox.com>,
Adal Chiriliuc <me@spammers.com> wrote:
我认为应该在
内置模块中添加类似下面的函数:

def boolselect(condition,trueresult,falseresult):<如果条件:
返回trueresult
否则:
返回falseresult

[...]
您怎么看?

PS:也许可以找到比boolselect更好的名字。
I think a function similar to the one below should be added to the
builtin module:

def boolselect(condition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult

[ ... ]
What do you think?

PS: maybe a better name than boolselect could be found.




有些人可能会因为它的作用而感到不安

this_line = boolselect(dummy_flag," Dummy line \ n",infile.readline())

副作用是简单函数的主要原因

通常不能取代完整的条件三元运算符。

请参阅google进行三元运算符辩论,并在clp中查看其他

讨论现在。


问候。 Mel。



Some poeple might be disturbed by what it would do with

this_line = boolselect (dummy_flag, "Dummy line\n", infile.readline())

Side-effects are the main reason that a simple function
can''t generally replace a full conditional ternary operator.
See google for the ternary operator debate, and see the other
discussion in clp right now.

Regards. Mel.


Adal Chiriliuc< me@spammers.com>写道:
Adal Chiriliuc <me@spammers.com> writes:
我认为应该在
内置模块中添加类似下面的函数:

def boolselect(condition,trueresult,falseresult):<如果条件:
返回trueresult
否则:
返回falseresult
I think a function similar to the one below should be added to the
builtin module:

def boolselect(condition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult




这不起作用因为两个结果都得到了评估两种方式。例如,


boolselect(x == 0,f(x),g(x))

调用f和g。你需要类似


(lambda:g(x),lambda:f(x))[bool(condition)]()



That doesn''t work because both results get evaluated either way. E.g.

boolselect(x==0, f(x), g(x))

calls both f and g. You need something like

(lambda: g(x), lambda: f(x))[bool(condition)]()


这篇关于提案:模拟C?:运算符的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆